Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know when an Android ListView is done being populated

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!

like image 888
mahdaeng Avatar asked Oct 28 '10 02:10

mahdaeng


People also ask

How check ListView is empty?

just check if (cartlist. size()<0) then yours list is Empty.!

Does ListView recycle?

ListView is a view which groups several items and displays them in a vertical list. This list is also automatically made scrollable if the amount of data supplied cannot be accommodated on the screen. ArrayAdapter and ListView are required for view recycling.

How do you populate a ListView?

To add rows to a ListView you need to add it to your layout and implement an IListAdapter with methods that the ListView calls to populate itself. Android includes built-in ListActivity and ArrayAdapter classes that you can use without defining any custom layout XML or code.

Which function is automatically called when the list item view is ready to be displayed?

The ListView instance calls the getView() method on the adapter for each data element. In this method the adapter creates the row layout and maps the data to the views in the layout. This root of the layout is typically a ViewGroup (layout manager) and contains several other views, e.g., an ImageView and a TextView .


1 Answers

You can use a Handler to accomplish this task, like this:

In your activity add the Handler as any other property.

private Handler mListViewDidLoadHanlder = new Handler(new Handler.Callback() { 
    @Override
    public boolean handleMessage(Message message) {
        //Do whatever you need here the listview is loaded
        return false;
    }
});

And inside the getView method of your listview adapter you do the comparison to see if the current position is the last one , so , it will finish (just put it before the return):

public View getView(int position, View convertView, ViewGroup parent) {
         //Your views logic here

         if (position == mObjects.size() - 1) {
                mViewDidLoadHanlder.sendEmptyMessage(0);
            }

         return convertView;
     }
like image 55
Renan Grativol Avatar answered Oct 01 '22 02:10

Renan Grativol