1

Say I have a ListView in android and its adapter is bound to an arraylist holding two elements.

However I find a question that when I start the activity in which the ListView is located,the getView method of the arrayAdapter is called for many times (at least 4 times).However my arraylist only holds 2 elements.And actually only two elements appear on the screen finnaly.

So here is my question:why the getView method is called much more times than it should be?

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
            ViewGroup parent) {
        View rowView=convertView;
        if(rowView==null)
        {
            Log.d("rowview==null is true", Integer.toString(childPosition));
                    }
        else{
            Log.d("rowview==null is false",  Integer.toString(childPosition));
        }

There are about 4 Log in LogCat .From my point of view ,it should be 2.

Any help is appreciated.

Andrew Carl
  • 802
  • 1
  • 8
  • 15
  • Check : http://stackoverflow.com/questions/2618272/custom-listview-adapter-getview-method-being-called-multiple-times-and-in-no-co – Haresh Chhelana Aug 28 '14 at 12:19

1 Answers1

1

The items can be requested first for measure/layout purposes, and then later again to be actually attached to the list view.

If your listview is in a layout that requires multiple measure/layout passes, such as LinearLayout with weights, you'll get even more requests for views in your adapter.

To learn why the getView() method is called, put a debugger breakpoint there and examine the call stack.

laalto
  • 150,114
  • 66
  • 286
  • 303