Apps 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?
 

alostpacket

Over Macho Grande?
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...
 

That Don Guy

Lurker
Thread starter
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.
 

alostpacket

Over Macho Grande?
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:
 [URL="http://developer.android.com/reference/android/widget/SimpleAdapter.html#getView%28int,%20android.view.View,%20android.view.ViewGroup%29"]getView[/URL](int position, [URL="http://developer.android.com/reference/android/view/View.html"]View[/URL] convertView, [URL="http://developer.android.com/reference/android/view/ViewGroup.html"]ViewGroup[/URL] parent)
on the adapter and passing null for the convertview

Something like
Code:
View myRowView = mySimpleAdapter.getView(0, null, myListView);
hth :)
 

That Don Guy

Lurker
Thread starter
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.
 
Top