When rewriting baseadapter, we know that we need to rewrite the following four methods: Getcount,getitem (int position), getitemid (int position), GetView method,
GetCount determines how many items the ListView has, and GetView returns the view displayed for each item item.
But getitem (int position), what does getitemid (int position) do? How do you rewrite it?
First Look at GetItem:
@Override
Public Object getItem (int position) {
. ...
}
The official explanation is that get the data item associated with the specified position in the set. That is, you get the item in the corresponding data collection at a specific location. So where is the method called? When is it called?
By looking at the source code discovery, the GetItem method is not called in the Baseadapter class, but is called in Adapterview.
In the Adapterview class, we found the following method,
Public Object getitematposition (int position) { T adapter = Getadapter (); Return (adapter = = NULL | | Position < 0)? Null:adapter.getItem (position); }
So when did getitematposition (position) be called? Answer: It will not be automatically called, it is used in our setting
Setonitemclicklistener, Setonitemlongclicklistener, and Setonitemselectedlistener are easily invoked in the click-to-Select handling event
To get the current row data. Official explanation Impelmenters can call getitematposition (position) if they need to access the data
Associated with the selected item. So in general, we can write this:
@Override
Public Object getItem (int position) {
return This.datalist.get (position);
}
Of course, if you like, you can also return null directly inside.
As for Getitemid(int position), it returns a similar method for the id,adapterview of the postion corresponding item:
public long getitemidatposition (int position) { T adapter = Getadapter (); Return (adapter = = NULL | | Position < 0)? INVALID_ROW_ID:adapter.getItemId (position); }
Unlike GetItem, some methods (such as the OnClick method of Onclicklistener) have the ID parameter, which is determined by the return value of Getitemid ().
We can generally achieve this:
@Override public long getitemid (int position) { return position; }