How does the getView() method work when creating your own custom adapter? How does the getView() method work when creating your own custom adapter? android android

How does the getView() method work when creating your own custom adapter?


1: The LayoutInflater takes your layout XML-files and creates different View-objects from its contents.

2: The adapters are built to reuse Views, when a View is scrolled so that is no longer visible, it can be used for one of the new Views appearing. This reused View is the convertView. If this is null it means that there is no recycled View and we have to create a new one, otherwise we should use it to avoid creating a new.

3: The parent is provided so you can inflate your view into that for proper layout parameters.

All these together can be used to effectively create the view that will appear in your list (or other view that takes an adapter):

public View getView(int position, @Nullable View convertView, ViewGroup parent){    if (convertView == null) {        //We must create a View:        convertView = inflater.inflate(R.layout.my_list_item, parent, false);    }    //Here we can do changes to the convertView, such as set a text on a TextView     //or an image on an ImageView.    return convertView;}

Notice the use of the LayoutInflater, that parent can be used as an argument for it, and how convertView is reused.


getView() method in Adapter is for generating item's view of a ListView, Gallery,...

  1. LayoutInflater is used to get the View object which you define in a layout xml (the root object, normally a LinearLayout,FrameLayout, or RelativeLayout)

  2. convertView is for recycling. Let's say you have a listview which can only display 10 items at a time, and currently it isdisplaying item 1 -> item 10. When you scroll down one item, theitem 1 will be out of screen, and item 11 will be displayed. Togenerate View for item 11, the getView() method will be called, andconvertView here is the view of item 1 (which is not neccessaryanymore). So instead create a new View object for item 11 (which iscostly), why not re-use convertView? => we just check convertView isnull or not, if null create new view, else re-use convertView.

  3. parentView is the ListView or Gallery... which contains the item's view which getView() generates.

Note: you don't call this method directly, just need to implement it to tell the parent view how to generate the item's view.


You could have a look at this video about the list view. Its from last years Google IO and still the best walk-through on list views in my mind.

http://www.youtube.com/watch?v=wDBM6wVEO70

  1. It inflates layouts (the xml files on your res/layout/ folder) into java Objects such as LinearLayout and other views.

  2. Look at the video, will get you up to date with whats the use of the convert view, basically its a recycled view waiting to be reused by you, to avoid creating a new object and slowing down the scrolling of your list.

  3. Allows you to reference you list-view from the adapter.