Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "convertView" parameter in ArrayAdapter getView() method

Tags:

java

android

Can someone tell me what the convertView parameter is used for in the getView() method of the Adapter class?

Here is a sample code take from here:

@Override public View getView(int position, View convertView, ViewGroup parent) {     View v = convertView;     if (v == null) {         LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);         v = vi.inflate(R.layout.row, null);     }     Order o = items.get(position);     if (o != null) {         TextView tt = (TextView) v.findViewById(R.id.toptext);         TextView bt = (TextView) v.findViewById(R.id.bottomtext);         if (tt != null) {             tt.setText("Name: "+o.getOrderName());                            }         if(bt != null){             bt.setText("Status: "+ o.getOrderStatus());         }     }     return v; } 

What should we pass via convertView?

What I've found, take from here:

Get a View that displays the data at the specified position in the data set. You can either create a View manually or inflate it from an XML layout file. When the View is inflated, the parent View (GridView, ListView...) will apply default layout parameters unless you use inflate(int, android.view.ViewGroup, boolean) to specify a root view and to prevent attachment to the root.

Parameters

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.

parent -- The parent that this view will eventually be attached to Returns

returns -- A View corresponding to the data at the specified position.

like image 854
Tarik Avatar asked Dec 07 '10 22:12

Tarik


People also ask

What is convertView?

convertView is the ListView Item Cache that is not visible, and hence it can be reused. It lets the ListView need not create a lot of ListItems, hence saving memeory and making the ListView more smooth.

Which is the 3rd argument for ArrayAdapter?

The third parameter is textViewResourceId which is used to set the id of TextView where you want to display the actual text.

What is use of ArrayAdapter in Android?

In Android development, any time we want to show a vertical list of scrollable items we will use a ListView which has data populated using an Adapter . The simplest adapter to use is called an ArrayAdapter because the adapter converts an ArrayList of objects into View items loaded into the ListView container.


1 Answers

You shouldn't be calling that method by yourself.

Android's ListView uses an Adapter to fill itself with Views. When the ListView is shown, it starts calling getView() to populate itself. When the user scrolls a new view should be created, so for performance the ListView sends the Adapter an old view that it's not used any more in the convertView param.

like image 150
Macarse Avatar answered Oct 05 '22 15:10

Macarse