HomeHealth basics for Android Projects 1: Baseadapter,
When rewriting Baseadapter, we know that the following four methods need to be rewritten: getCount, getItem (int position), getItemId (int position), getView method,
GetCount determines the total number of items in listview, while getView returns the view displayed for each item.
But what is the role of getItem (int position) and getItemId (int position? How can I rewrite it?
First, let's look at getItem:
@ Override
Public Object getItem (int position ){
....
}
The official explanation is to Get the data item associated with the specified position in the data set. That is, to Get the data item at a specific position in the corresponding dataset. So where is this method called? When will it be called?
By checking the source code, it is found that the getItem method is called not in the Baseadapter class, but in the Adapterview.
In the adapterView class, we find the following method,
public
Object getItemAtPosition(
int
position) {
T adapter = getAdapter();
return
(adapter ==
null
|| position <
0
) ?
null
: adapter.getItem(position);
}
So when is getItemAtPosition (position) called? Answer: it will not be automatically called. It is used to set
SetOnItemClickListener, setOnItemLongClickListener, and setOnItemSelectedListener
To obtain the data of the current row. Official Description: Impelmenters can call getItemAtPosition (position) if they need to access the data
Associated with the selected item. Therefore, we can write as follows:
@ Override
Public Object getItem (int position ){
Return this. datalist. get (position );
}
Of course, if you like it, you can also directly return null in it.
As for getItemId (int position), it returns the id of the item corresponding to the postion, and adapterview has a similar method:
public
long
getItemIdAtPosition(
int
position) {
T adapter = getAdapter();
return
(adapter ==
null
|| position <
0
) ? INVALID_ROW_ID : adapter.getItemId(position);
}
Different getItem methods (such as onclicklistener's onclick method) have the id parameter, which depends on the returned value of getItemId.
We can generally achieve this:
@Override
public
long
getItemId(
int
position) {
return
position;
}