views:

42

answers:

2

I have a child Activity that contains a ListView. This Activity is populated asynchronously from a SQLite cursor. The list items contain a TextView, a RadioButton, and a normal Button. The XML is shown below:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/rlCategoryListItemLayout" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content">
 <TextView android:id="@+id/tvCategoryListItemTitle" style="@style/uwLargeListItemLabelStyle" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:singleLine="true" android:text="This is a test note title" />
 <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_alignParentRight="true" android:gravity="center_vertical">
  <RadioButton android:id="@+id/rdoCategoryListItemSelect" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginRight="10dip" />
  <Button android:id="@+id/btnCategoryListItemDelete" android:background="@drawable/delete_red" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" />
 </LinearLayout>
</RelativeLayout>

I have to do some logic to determine which RadioButton is selected by default, and I can't do it until the ListView is already loaded. The problem is that with all the events I have tried so far (onCreate, onPostCreate, onResume, onWindowFocusChanged), the ListView child count is zero. I've also tried using the getView method in the ArrayAdapter class, but that method is called mutliple times and the ListView child count is potentially different every time, leading to unexpected results. Apparently, these events are firing before the ListView has finished being completely populating with its child items.

Is there an event I can listen for, or some other way to determine when the ListView is finished populating and has all of its children accessible to be modified programmatically?

Thank you!

A: 

A ListView does not actually create a View for each element in the list of data it is rendering. Instead, it recycles views in order to be responsive and not hog memory. You might rethink what you really need when you say the ListView needs to be populated.

It might help if you go into a little more detail about your app and post some code.

elevine
Thanks, elevine. I just have to select one of the RadioButtons by default. When I try to do that, though, the ListView doesn't seem to have child items yet. So, I need to know when the ListView is fully populated so I can go in and select one of the list items.
mahdaeng
My point is what you mean by "ListView is fully populated" is not clear. If by that you mean that all of the items in the list are visible, then that condition might never happen given the way ListView renders rows.
elevine
Yes - that's exactly what I mean. :^(
mahdaeng
A: 

At the end of your asynchronous work on the database, you can use Handler.post(Runnable) to run code back on the main thread. Its almost like triggering a callback and you can guarantee that it will run after the list is populated. You can even schedule the Runnable to occur at a certain time later if you need to time everything very carefully.

Was that helpful?

mtmurdock
Thank you, mtmurdock. I'll try some things there and see what I can come up with. I'll report back on my results.
mahdaeng
Another option along those lines is to use AsyncTask: http://developer.android.com/reference/android/os/AsyncTask.html
elevine
As it turned out, I had a subtle bug elsewhere in my code. Apparently, I was firing off my Runnable exactly when I should have been, but the other bug was making it appear that certain controls were not ready yet. Thank you, mtmurdock and elevine, for your willingness to help me out with this.
mahdaeng