Android Custom Listview

I went through the textbooks and searched, but still I can’t understand how,

getView(int position, View convertView, ViewGroup arg2) 

works when extends BaseAdapter creates a custom listView in my android application. Therefore, I cannot edit the list of user lists exactly the way I want.

I need to know when this method also calls parameter values.

If someone can explain the following method, this is excellent. 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; } 
+3
source share
3 answers

getView () . As indicated in the specifications, the getView method displays data at the specified position. That way, when you install the adapter and when you scroll through your listView method, getView is called.

The method you copied here is part of the EfficientAdapter to optimize the performance of the ListView and along with the optimization, you used the ViewHolder template.

Copied to specs: with fewer explanations

position : the position of the element in the adapter dataset of the element we want to see.

convertView . Old view for reuse if possible. Note. You must verify that this is using an invalid representation and appropriate type before use. If it is not possible to convert this view to display the correct data, this method can create a new view. Heterogeneous lists can indicate their number of view types, so this view always has the correct type (see GetViewTypeCount () and getItemViewType (int)).

So, in the above method, when you do the following, you reuse the converted one.

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

And by doing the following, you avoid searching (findViewById), that’s what’s good in the ViewHolder template

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

parent : parent element that will eventually be bound to

Edited

Question: How many times getView is called and how many convertView will be created? Answer: Let's take an example of the Efficeint Adapter from ApiDemos . If 10 lines are displayed on your screen, then

convertView Count : 10 + 1 = 11 (10 Builds what you see on the screen, another one to show the scroll effect). This means that if (convertView == null) {...} statements will only be called 11 times.

getView Count . Initially, the count will be 10, but when you start to increase the scroll counter. getView is called every time to update the data.

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

+3
source

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

Position - The position of the element in the dataset of the adapter of the element whose representation we want.

convertView - An old view of reuse, if possible. Note. Before use, make sure that this view is not null and of the appropriate type. If it is not possible to convert this view to display the correct data, this method can create a new view. Heterogeneous lists may indicate their number of view types, so this view always has the correct type

ViewGroup - This view will eventually be attached to.

+4
source

Here is a description of the getView() parameters:

int position - view position in the list;

View convertView - IMHO, this is the most difficult parameter to understand. At the beginning of the list, convertView = null. But when you start to scroll down, when the list item (which is an instance of the View ) is hidden, it is stored in memory, for example, convertView . This trick allows you to not create a new element when scrolling backward , but use the convertView stored in memmory. So the first element of the list that becomes convertView is the element at position 0. Remember that when you scroll down the ListView (from 0 to a larger one), convertView is up and down if you scroll up the ListView.

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

ViewHolder is a template that provides convenient communication with list items. You make this object an element tag and you can use it to indirectly interact with the list element because it refers to the fields of the element ( View.setTag(holder) ).

The getView () method is called every time Android needs to draw another list item.

Have questions?

+2
source

Source: https://habr.com/ru/post/907119/


All Articles