4

I went through tutorials and searched, but still I can't understand how the,

getView(int position, View convertView, ViewGroup arg2)

method works when extends BaseAdapter to create a custom listView in my android application. Therefore I cant Edit the Custom list view exactly I want.

I need to know when this method invokes and the meanings of the parameters.

If someone can explain the following method its great. Thanks

@Override
public View getView(int position, View convertView, ViewGroup arg2)
{

    ViewHolder holder;
    LayoutInflater inflater =  context.getLayoutInflater();

    if (convertView == null)
    {
        convertView = inflater.inflate(R.layout.listitem_row, null);                     
        holder = new ViewHolder();
        holder.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
        holder.txtViewDescription = (TextView) convertView.findViewById(R.id.textView2);

        convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();                                        
    }

    holder.txtViewTitle.setText(title[position]);
    holder.txtViewDescription.setText(description[position]);



  return convertView;
}
JibW
  • 4,538
  • 17
  • 66
  • 101

3 Answers3

4

getView() is called when you call setAdapter in your code. After that when you move focus over list or select any item or you call notifyDataSetChanged() , you get call in getView().

Position - The position of the item within the adapter's data set of the item whose view we want.

convertView - The old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view. Heterogeneous lists can specify their number of view types, so that this View is always of the right type

The ViewGroup - that this view will eventually be attached to.

Dori
  • 915
  • 1
  • 12
  • 20
Vineet Shukla
  • 23,865
  • 10
  • 55
  • 63
  • Hi vineetska Thanks for the answer. In my code when I want to load this list, getView() method invokes three times of the number of records in the List view. I checked it by creating a log.. Can't understand why this is so – JibW Aug 12 '11 at 12:33
  • use " LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);" inside if block in getView() in place of "LayoutInflater inflater = context.getLayoutInflater(); " – Vineet Shukla Aug 12 '11 at 13:03
  • Still that is same way. Bit confusing with this method..Can't really figure out the execution.. number of times executing this method is three times of the records(When 2 listview records display in the list, this method runs 6 times) – JibW Aug 12 '11 at 14:13
3

getView() : As mentioned in specs getView method displays the data at the specified position. So, when you setAdapter and when you scroll your listView getView method will be called.

The method you copied here is a part of EfficientAdapter to optimize your ListView performance and along with optimization you used ViewHolder pattern.

Copied from Specs : With little more explanation

position :The position of the item within the adapter's data set of the item whose view we want.

convertView: The old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view. Heterogeneous lists can specify their number of view types, so that this View is always of the right type (see getViewTypeCount() and getItemViewType(int)).

So, in above method when you are doing the following thing you are reusing your convertView.

     if (convertView == null){
            ....
            convertView.setTag(holder);
     } else {
           holder = (ViewHolder) convertView.getTag(); 
      }

And by doing following thing you are avoiding lookup (findViewById), thats what the good thing about ViewHolder pattern

      holder.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);

parent : The parent that this view will eventually be attached to

Edited

Question : How many times getView is called and how many convertView will be created ? Answer: Lets take an example of Efficeint Adapter from ApiDemos. If your screen showing 10 Rows, then,

convertView Count : 10 + 1 = 11 (10 Rows what you are seeing on screen, one extra to show scrolling effect). That means statements in if(convertView == null){...} block will be called only 11 times.

getView Count: Initially count will be 10, but when you start scrolling count keep on increasing. getView called every time to update data.

Note: This is only true for getView method mentioned in question.

Gopal
  • 1,719
  • 12
  • 13
  • yes, but when I load the items in to listview using this method, the getView() method runs three times of the number of elements that going to display. I can't Understand why...... – JibW Aug 12 '11 at 15:37
  • And at the same time after it create convertView = inflater.inflate(R.layout.listitem_row, null) for the first time, when it loads items it runs more than once. Can you please explain why??? – JibW Aug 12 '11 at 15:38
  • @JibW sure, It depends on how much rows you are visually seeing on your screen. Suppose you are seeing only 3 rows, that means ListView need to show only 3 convertView, and when you scroll them those 3 will be reused. Because at a time you are not showing more then that on your screen. Thats why getView will be called 3 times. Try HierarchyViewer it will help you understand more about number of ConvertViews. Hope this answer your question...if not ping me...I will be glad to help you out. – Gopal Aug 12 '11 at 15:51
  • Hi gopal Thank you very much. But I tested with 4 elements to be load to the listview. So getview() method executed 12 times. Everytime it runs 3 times than the number of elements displaying in the listview.. Bit confused...! – JibW Aug 12 '11 at 16:01
  • @JibW try to put your counter inside if (convertView == null) { //your count statement }. Number should drop. As I mentioned in my answer, getView method is called when you setAdapter and when you scroll. Try this, let me know if still you are getting that weird number... – Gopal Aug 12 '11 at 16:09
  • When I put my counter inside (convertView == null) { }, It runs 5 times when it have to load 4 elements to the ListView.. – JibW Aug 12 '11 at 16:19
  • Sorry, I forgot to mention in my previous comment, it will create one extra view, to show you scrolling effect. – Gopal Aug 12 '11 at 16:21
2

Here is the description of getView() parameters:

int position - the position of view in the list;

View convertView - IMHO, this is the most difficult parameter for understanding. At the beginning of list's work convertView = null. But when you start to scroll it down, when an item of list (which is instance of View) is hidden, it is stored in memory like convertView. This trick allows you not to create a new item when you scroll your list back, but use convertView stored in memmory. So the first item of list which becomes the convertView is the item at 0 position. Remember, that when you scroll your ListView down (from 0 position to bigger), convertView is situated on the top and on the bottom if you scroll the ListView up.

ViewGroup arg2 - this is your ListView (this class is derived from ViewGroup).

ViewHolder is the pattern, that makes comfortable communication with list's items. You make this object as item's tag and can use them for for indirect interaction with list's item, because it refers on item's fields (View.setTag(holder)).

getView() method is called every time when Android need to draw another one list's item.

Any questions?

teoREtik
  • 7,886
  • 15
  • 46
  • 65
  • Thanks teoREtik... when I load the items in to listview using this method, the getView() method runs three times of the number of elements that going to display. (for example getView() method runs 6 times if it has to load 2 elements to the listview),I cant Understand why........ also after it create convertView = inflater.inflate(R.layout.listitem_row, null), by checking convertview==null it runs several times while loading elements to the listview – JibW Aug 12 '11 at 15:48