Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

My questions are:

  1. What is exactly the function of the LayoutInflater?
  2. Why do all the articles that I've read check if convertview is null or not first? What does it mean when it is null and what does it mean when it isn't?
  3. What is the parent parameter that this method accepts?
like image 822
GrowinMan Avatar asked Apr 12 '12 08:04

GrowinMan


People also ask

What is getView method in Android?

getView(int position, View convertView, ViewGroup parent) Get a View that displays the data at the specified position in the data set. abstract int. getViewTypeCount() Returns the number of types of Views that will be created by getView(int, View, ViewGroup) .

Which of the following method you must implement when creating a list adapters?

To use the BaseAdapter with a ListView, a concrete implementation the BaseAdapter class that implements the following methods must be created: int getCount() Object getItem(int position) long getItemId(int position)

How do you connect an adapter with ListView write down the codes for it?

Attaching the Adapter to a ListView // Construct the data source ArrayList<User> arrayOfUsers = new ArrayList<User>(); // Create the adapter to convert the array to views UsersAdapter adapter = new UsersAdapter(this, arrayOfUsers); // Attach the adapter to a ListView ListView listView = (ListView) findViewById(R. id.


2 Answers

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.

like image 180
Jave Avatar answered Oct 10 '22 06:10

Jave


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 is displaying item 1 -> item 10. When you scroll down one item, the item 1 will be out of screen, and item 11 will be displayed. To generate View for item 11, the getView() method will be called, and convertView here is the view of item 1 (which is not neccessary anymore). So instead create a new View object for item 11 (which is costly), why not re-use convertView? => we just check convertView is null 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.

like image 28
Anh Tuan Avatar answered Oct 10 '22 07:10

Anh Tuan