How android getView() works in BaseAdapter?

860 Views Asked by At

I've read some articles and watched video in order to understand how ListView works. So, let's say I have list with 5 items (all items visible on screen). I have the following code in my custom adapter which inherited from BaseAdapter:

@Override
public View getView(int position, View view, ViewGroup parent) {
    System.out.println("item position: " + position + " view " + view);

    if (view == null)
    {
        view = LayoutInflater.from(context).inflate(R.layout.list_view_item, null);
    }
    return view;
}

When I run this code I see the following output in console:

08-26 14:52:40.882: item position: 0 view null
08-26 14:52:40.883: item position: 1 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.883: item position: 2 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 3 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 4 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 0 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 1 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 2 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 3 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 4 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 0 view android.widget.RelativeLayout{88bc756 V.E...C.. ......I. 0,0-1080,65 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 1 view android.widget.RelativeLayout{1264e60 V.E...C.. ......I. 0,68-1080,133 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 2 view android.widget.RelativeLayout{36b089 V.E...C.. ......I. 0,136-1080,201 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 3 view android.widget.RelativeLayout{e1a12bb V.E...C.. ......I. 0,204-1080,269 #7f0e00b8 app:id/item_container}
08-26 14:52:40.885: item position: 4 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}

I getView() view is null only for the 0 position. After watching I supposed that I had null for the first 5 calls (until all visible item views not initialized). Why only for the first? What I am missing?

2

There are 2 best solutions below

0
Melv_80 On

This is a factory method for the view. To prevent creating complex views over and over again you may return the created view and received it later on as parameter by the frame work.

You are supposed to check, wether you can reuse your view or not (e.g. the type matches, if you have different line visualisations) and then simply use the view for performance reasons instead of creating a new one (inflate a new one).

View inflation involves parsing XML. and is a costly operation. Thats why the API allows you to reuse components. It is a common pattern on UI development.

That is why you are having null on the first calls, and later on an instance of the view.

2
Amilcar Andrade On

ListView recycles views so the first run it will be null because you have to inflate the view. Once you inflate the view you should be reusing the same view and just updating the contents in order to improve performance. I would recommend watching the following video https://www.youtube.com/watch?v=wDBM6wVEO70 , they explained really well how list view works.