Search code examples
javaandroidexpandablelistviewexpandablelistadapter

Why is .getChildCount() always zero for my ExpandableListView?


I have an ExpandableListView, declared as:

private ExpandableListView mELV_PredefinedFruit;

and uses an adapter which extends BaseExpandableListAdapter. It displays onscreen correctly with various children in 2 groups (both expanded). However I always get zero which I check the number of children at the end of the Activity onCreate using the following:

int numberOfChildViews = mELV_PredefinedFruit.getChildCount();
Log.d(LOG_CLASS, String.format(Locale.US, "%s number of child views in ELV:  %d", METHOD, numberOfChildViews));

Why is it always zero?


Solution

  • You are getting 0, because your view is not yet ready. Try this:

    mELV_PredefinedFruit.post(new Runnable() {
        @Override
        public void run() {
            int numberOfChildViews = mELV_PredefinedFruit.getChildCount();
            Log.d(LOG_CLASS, String.format(Locale.US, "%s number of child views in ELV:  %d", METHOD, numberOfChildViews));
        }
    });