Mr Stone
Mr Stone

Reputation: 35

Android: system call getView twice when activity created

I have a custom list with about 20 item and three lines each item. I used a Log to trace how getView method is called

public View getView(int position, View convertView, ViewGroup parent) {

    Log.e("getView", "at position " + position);
    View view = convertView;

    if (view == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        view = inflater.inflate(resourceId, parent, false);
    }
            // my code here

}

Then, when list item loaded, i received this log

"getView", "at position 0"; "getView", "at position 1"; "getView", "at position 2"; "getView", "at position 3"; "getView", "at position 0"; "getView", "at position 1"; "getView", "at position 2"; "getView", "at position 3"; "getView", "at position 4"; "getView", "at position 5"; ..............

Can anyone tell me why getView method called twice from 0 to 3 ?

Upvotes: 2

Views: 867

Answers (1)

user
user

Reputation: 87064

Can anyone tell me why getView method called twice from 0 to 3?

The getView method is called twice because the ListView , as part of it's onMeasure method, calls the getView method of the adapter to get the row's View to see how big they are. I guess you have three visible rows, so the ListView will call the getView method three times for this first three visible children.

Upvotes: 1

Related Questions