Several common methods of Android adapter are shared with the following details
1 When is the ListView set up to monitor the adapter data?
In Setadapter (ListAdapter adapter), the data listening in the original Madapter in the ListView is cancelled first (Madapter.unregisterdatasetobserver ( Mdatasetobserver), and then set the adapter data listening for the new settings.
2 GetView (int position, View Convertview, ViewGroup parent)
We all know that the GetView method of Madapter is very important, so how is the method used in ListView? In the ListView source code did not find the GetView method call, so we go to listview the parent class Abslistview. GetView is called in the Obtainview in Abslistview, and its main code logic section is:
View Obtainview (int position, boolean[] isscrap) {
isscrap[0] = false;
View Scrapview;
Get view
Scrapview = Mrecycler.getscrapview (position) from the collector;
View child;
if (Scrapview!= null) {
...
If not empty, then the Convertview is passed in so that the view is reused, and the data is updated with child
= Madapter.getview (position, Scrapview, this);
...
} else {
//if empty, recreate the Holderview in GetView and fill in the data child
= Madapter.getview (position, NULL, this);
...
}
return child;
}
And Obtainview will be used in the measure of ListView and the generation of the whole ListView.
The final thing to rewrite the GetView method is to remember that Convertview is reused, and that no reuse will almost always result in memory unloading.
3 GetCount ()
What does adapter's GetCount () do for? In ListView, the GetCount () function of adapter is used in the process of onmeasure and touch distribution responses. There is no doubt: it should return the number of data in the underlying data.
4 GetItem (int position)
The
GetItem () is invoked in Adapterview and is then invoked by the user: we can see from the description of the two functions that we should return the position corresponding data in the adapter GetItem () method. But not necessarily to return the data that is used to display on the item's view, this still depends on the requirements, although most of the data may be returned from view.
/** * Gets The data associated with the specified in the list.
* * @param position Which data to get * @return the ' data associated with the ' specified position in the list * * *
Public Object getitematposition (int position) {T adapter = Getadapter (); Return (adapter = = NULL | | Position < 0)?
Null:adapter.getItem (position);
}/** * @return the data corresponding to the currently selected item, or * NULL if there are nothing selected.
*/Public Object GetSelectedItem () {T adapter = Getadapter ();
int selection = Getselecteditemposition (); if (adapter!= null && adapter.getcount () > 0 && selection >= 0) {return Adapter.getitem (Sele
ction);
else {return null; }
}
Throughout the structure, it can be said that there are three layers: datalists (the original underlying data)--adapter--adapterview, with the GetItem () method, we can directly use the Adapter to obtain data, Instead of getting a reference to the underlying datalists, with the existence of the Getitematposition () method, we can get the underlying data directly from the Adapterview without having to get a reference to its adapter. In this way, the simplicity of programming and decoupling is much better.
5 getitemid (int position)
Some of its calls are found in Adapterview,
public long getitemidatposition (int position) {T adapter = Getadapter (); Return (adapter = = NULL | | Position < 0)?
INVALID_ROW_ID:adapter.getItemId (position);
private void fireonselected () {if (Monitemselectedlistener = null) return;
int selection = This.getselecteditemposition ();
If (selection >= 0) {View v = getselectedview (); The return value obtained by the getitemid here and the selection are all part of the same item, the meaning of which is to get the ID of the item directly in the Onitemselected method of the selection interface. Instead of acquiring adapter to indirectly implement monitemselectedlistener.onitemselected (this, V, Selection, Getadapter (). Getitemid (sel
ection));
else {monitemselectedlistener.onnothingselected (this);
an int findsyncposition () {... rowId = Adapter.getitemid (seed);
if (rowId = = Idtomatch) {//From this point of view, Getitemid seems to be expected to return different values for different item, to maintain uniqueness//Found it!
return seed;
}
...
}
As with the GetItem () method analyzed above, both getitemid () and getitemidatposition () provide the convenience of programming. But for now, because there is no need for IDs, so most of the rewrite Getitemid method is a direct return of the position value, this is also true, although from the data acquisition does not make any sense (I give you a position, you return intact to me, what meaning). But I want to explain, do not be limited by this practice, and think Itemid is the item in the data position. In fact, if there is a need, you can use the Getitemid () method to return some other values, such as each item data in the database ID value, or each person's ID number.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.