Football Fans: Download the 2012 Schedule App from Google Play!


Go Back   Android Forums > Android Development > Application Development

Application Development Dev Lounge for the Coder Folks



Reply
 
LinkBack Thread Tools
Old December 30th, 2011, 12:20 PM   #1 (permalink)
New Member
 
Join Date: Feb 2011
Posts: 7
 
Device(s):
Thanks: 0
Thanked 1 Time in 1 Post
Default Initalizing items in a ListView?

I am implementing a ListView by changing the selected item's background in OnItemClick. However, I am not able to "initialize" an item in the app's OnCreate. (I tried using <ListView>.getChildAt(0).setBackgroundColor, but inside of OnCreate, getChildAt(0) returns null.)

How do I "initialize" items in a ListView?

That Don Guy is offline  
Reply With Quote
Sponsors
Old December 30th, 2011, 02:34 PM   #2 (permalink)
Over Macho Grande?
 
alostpacket's Avatar
 
Join Date: Nov 2009
Location: NY
Posts: 7,090
 
Device(s): GalaxyNexus(LTE), NexusOne, OG Droid, GalaxyTab 10.1(LTE), Eris, Logitech Revue (fishtank)
Thanks: 4,164
Thanked 3,126 Times in 1,292 Posts
Default

Why would you initialize the item? Usually the framework will lay it out (aka "inflate" it)

Basically, can you clarify what you're trying to accomplish? Also how do you add you items to the listview? Are you using a ListActivity? ListFragment? ListView + adapter? etc...
alostpacket is offline  
Reply With Quote
Old December 30th, 2011, 03:25 PM   #3 (permalink)
New Member
 
Join Date: Feb 2011
Posts: 7
 
Device(s):
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
Originally Posted by alostpacket View Post
Why would you initialize the item? Usually the framework will lay it out (aka "inflate" it)
I am trying to use a ListView to select one or more items, and I want one of them to be already selected when it first appears.

The list is populated from a string-array in values/strings.xml.

Here is the .xml for the layout:
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/select_location"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent" 
> 
    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Select Location:"
    />
    <ListView android:id="@+id/location_list"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_weight="0.5" 
    />
</LinearLayout>
and here is a portion of the app's main .java file:
Code:
String[] Locations;
ListView LocationList;
ArrayList<HashMap<String, String>> LocationAndTypeList = new ArrayList<HashMap<String, String>>(32);
int SelectedLocation;
    
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
        
    // Populate select_location
    // R.array.location points to a StringArray of 25 location names
    Location = getResources().getStringArray(R.array.location);

    String[] FromList = new String[] {"text1", "text2"};
    int[] ToList = new int[] {R.id.text1, R.id.text2};

    for (int i = 0; i < Locations.length; i++)
    {
        HashMap<String, String> M = new HashMap<String, String>();
        M.put("text1", Locations[i]);
        M.put("text2", "Row " + Integer.toString(i + 1));
        LocationAndTypeList.add(M);
    }

    // Populate the list view
    LocationList = (ListView)findViewById(R.id.location_list);
    SimpleAdapter LocationListAdapter = new SimpleAdapter(this, LocationAndTypeList, R.layout.twolinelistitemrow, FromList, ToList);
    LocationList.setAdapter((ListAdapter)LocationListAdapter);
    LocationList.setOnItemClickListener(new LocationListClickListener());

    // Set the initially selected value to -1
    SelectedLocation = -1;
}

//  When an item is clicked, change its background color
public class LocationListClickListener implements OnItemClickListener 
{
    public void onItemClick(AdapterView<?> parent, View view, int pos, long id) 
    {
        // "Unselect" the previously selected location
        if (SelectedLocation >= 0)
        {
            View previousLocation = parent.getChildAt(SelectedLocation);
            previousLocation.setBackgroundColor(Color.TRANSPARENT);
        }
        SelectedLocation = pos;
        view.setBackgroundColor(Color.rgb(0, 80, 0));
    }
}
What I want to do is, I want to have the first item in the ListView have its background changed to the "selected" color (RGB(0, 80, 0)) when it first appears.
That Don Guy is offline  
Reply With Quote
Old December 30th, 2011, 04:13 PM   #4 (permalink)
Over Macho Grande?
 
alostpacket's Avatar
 
Join Date: Nov 2009
Location: NY
Posts: 7,090
 
Device(s): GalaxyNexus(LTE), NexusOne, OG Droid, GalaxyTab 10.1(LTE), Eris, Logitech Revue (fishtank)
Thanks: 4,164
Thanked 3,126 Times in 1,292 Posts
Default

Does it work from the click method?

If so, you could try calling it from
Code:
onResume()
if it's returning null, you basically have a "race condition". You're trying to set the background color on something that hasn't been created yet.


You could also try calling
Code:
 getView(int position, View convertView, ViewGroup parent)
on the adapter and passing null for the convertview

Something like
Code:
View myRowView = mySimpleAdapter.getView(0, null, myListView);
hth
alostpacket is offline  
Last edited by alostpacket; December 30th, 2011 at 04:17 PM.
Reply With Quote
Old December 30th, 2011, 05:08 PM   #5 (permalink)
New Member
 
Join Date: Feb 2011
Posts: 7
 
Device(s):
Thanks: 0
Thanked 1 Time in 1 Post
Default

Thanks for the advice, but even in onResume() (as well as onStart()), getChildAt(0) returns null. In fact, in onResume(), getChildCount() returns zero.

It does work in the click condition, so it most likely is a case of trying to access something that just hasn't been created yet.
That Don Guy is offline  
Reply With Quote
Old December 30th, 2011, 05:43 PM   #6 (permalink)
Over Macho Grande?
 
alostpacket's Avatar
 
Join Date: Nov 2009
Location: NY
Posts: 7,090
 
Device(s): GalaxyNexus(LTE), NexusOne, OG Droid, GalaxyTab 10.1(LTE), Eris, Logitech Revue (fishtank)
Thanks: 4,164
Thanked 3,126 Times in 1,292 Posts
Default

You could try something like:

protected void onFinishInflate ()


View | Android Developers

But in order to use that callback you would need to subclass ListView
alostpacket is offline  
Reply With Quote
Reply

Bookmarks

Tags
initialize, listview, oncreate


Go Back   Android Forums > Android Development > Application Development User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 12:00 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo