Several common methods of Android adapter

Several common methods of Android adapter are shared with you. The specific contents are as follows

1. When did the listview set the data listening of the adapter?

In setadapter (listadapter adapter), data listening in the original madapter in listview will be cancelled first (madapter. Unregisterdatasetobserver (mdatasetobserver);), Then set the data listening of the newly set adapter.

2 getView(int position,View convertView,ViewGroup parent)

We all know that the getview method of the madapter is very important, so how is this method used in the listview? No call to getview method was found in the listview source code, so we went to the parent class abslistview of listview. GetView is called in obtainView in AbsListView. Its main code logic part is:

Obtainview will be used in the measure of listview and the generation of the whole listview.

For rewriting the getview method, the final thing is to remember the reuse of convertview. Without reuse, memory will almost be unloaded.

3 getCount()

What is the getcount () of the adapter used for? In listview, the getcount() function of the adapter is used in onmeasure and touch distribution response. There is no doubt that it should return the number of underlying data.

4 getItem(int position)

Getitem () is called in the AdapterView and then called by the user: from the description of these two functions, we can see that we should return the data corresponding to position in the getitem () method of the adapter, but it does not mean that we must return the data displayed on the view of the item. This depends on the demand, although it may return the data displayed in the view in most cases.

Looking at the whole structure, it can be said that there are three layers: datalists (original underlying data) -- adapter -- AdapterView. With the existence of getitem () method, we can directly use the adapter to obtain data without obtaining the reference of underlying datalists; With the existence of getitematposition () method, we can directly use AdapterView to obtain the underlying data without obtaining the reference of its adapter. In this way, it is much better for the simplicity and decoupling of programming.

5 getItemId(int position)

Some of its calls are found in AdapterView,

Like the getitem () method analyzed above, getitemid () and getitemidatposition () both provide programming convenience. However, at present, there is no need for ID, so most of them directly return the position value when rewriting the getitemid method. This is also right, although it makes no sense from the data acquisition (I give you a position, and you return it to me intact, what do you mean). However, I want to explain that we should not be limited by this practice and think that itemid is the position of item in the data. In fact, if there is a need, we can use getItemId () method to return some other values, such as the ID value of each item data in the database, or the ID number of each person.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>