先总的来说一下:getItem其实主要是在ListView中调用getItemAtPosition,getSelecttedItem中调用。
以前一直没关这个函数到底有什么用来着。今天特意看了下那些源码。发现在AdapterView中有两个地方调用了这个函数:
/**
* @return The data corresponding to the currently selected item, or
* null if there is nothing selected.
*/
public Object getSelectedItem() {
T adapter = getAdapter();
int selection = getSelectedItemPosition();
if (adapter != null && adapter.getCount() > 0 && selection >= 0) {
return adapter.getItem(selection);
} else {
return null;
}
}
/**
* Gets the data associated with the specified position 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);
}
现在可以看出来究竟是干嘛的了吧?其实就是你在之后要获取每个Item里面的具体数据的时候用的。这两个函数应该说和getItem是紧密相关的。比如说,有时候你需要知道哪个item在显示什么数据的时候,你获取到的数据其实就是你在getItem中实现的数据。
当然另外一个函数getItemId也是一样的,有个类似的调用如下:
public long getItemIdAtPosition(int position) {
T adapter = getAdapter();
return (adapter == null || position < 0) ? INVALID_ROW_ID : adapter.getItemId(position);
}