Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am getting listView.getChildCount(), is 0 after setting the arrayadapter?

Tags:

android

I want to load images from facebook and populate in listview, i am able to get list of friends and their info but i want to set image i am getting getChildCount() 0 , please help,

   public static ArrayList<FBUser> fbUserArrayList;
    public static Drawable sharedDrawable;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friends_list_view);
        this.setTitle("FB Friend List");

        final ListView listView = (ListView) findViewById(R.id.listview);
        FriendListAdapter couponsListAdapter = new FriendListAdapter(this,
                fbUserArrayList);
        listView.setAdapter(couponsListAdapter);

        couponsListAdapter.notifyDataSetChanged();


        setFbUsersImage(listView,fbUserArrayList);

    }

    private void setFbUsersImage(final ListView listView,final ArrayList<FBUser> fbUserArrayList) {
                // here am getting 0 ???
        for (int i = 0; i < listView.getChildCount(); i++) {
 ////           
   }

    }
like image 253
Indra KP Avatar asked Nov 30 '12 18:11

Indra KP


1 Answers

The ListView has not been drawn yet so it doesn't have any children. Unfortunately there is no callback, like onResume(), for when a View has been drawn, but you can use a Runnable to do what you want.


Addition
Let's make a couple changes in onCreate():

listView.setAdapter(couponsListAdapter);
// Remove your calls to notifyDataSetChanged and setFbUsersImage

// Add this Runnable
listView.post(new Runnable() {
    @Override
    public void run() {
        setFbUsersImage(listView,fbUserArrayList);
    }
});

You need to make listView a field variable just like fbUserArrayList. Now setFbUsersImage() will be run when the ListView has children.

All that said, if you are trying to modify the row in any way, the adapter is the best place to do this.

like image 55
Sam Avatar answered Sep 16 '22 14:09

Sam