Several common methods of Android Adapter are shared with you. The specific content is as follows
1 When does ListView set up data monitoring for Adapter?
In setAdapter(ListAdapter adapter), the data monitoring in the original mAdapter in the ListView will be cancelled ((mDataSetObserver);), and then the data monitoring of the newly set adapter will be set.
2 getView(int position, View convertView, ViewGroup parent)
We all know that the getView method of mAdapter is very important, so how is this method used in ListView? No call to getView method is found in the source code of ListView, so we go to the parent class AbsListView of ListView. GetView is called in obtainView in AbsListView, and its main code logic is:
View obtainView(int position, boolean[] isScrap) { isScrap[0] = false; View scrapView; //Get view from the recycler scrapView = (position); View child; if (scrapView != null) { ... //If it is not empty, then convertView is passed. In this way, the view is reused and the data is updated. child = (position, scrapView, this); ... } else { //If it is empty, recreate the HolderView in getView and fill in the data child = (position, null, this); ... } return child; }
The obtainView will be used in the ListView's measure and the generation of the entire ListView.
The ultimate thing to rewrite the getView method is to remember to reuse convertView. Without reuse, it will almost cause memory unloading.
3 getCount()
What is Adapter's getCount() used for? In ListView, Adapter's getCount() function is used in the onMeasure and touch distribution responses. There is no doubt that it should return the number of data in the underlying data.
4 getItem(int position)
getItem() is called in AdapterView and then used for user to call: From the description of these two functions, we can see that we should return the data corresponding to position in Adapter's getItem() method, but it does not mean that we must return the data used to display on the Item View. This depends on the requirements, although most cases may return the data displayed in the View.
/** * 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 : (position); } /** * @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 && () > 0 && selection >= 0) { return (selection); } else { return null; } }
Looking at the entire structure, it can be said that there are three layers: dataLists (original underlying data)--Adapter--AdapterView. With the existence of the getItem() method, we can directly use Adapter to obtain data without getting the reference to the underlying dataLists; with the existence of the getItemAtPosition() method, we can directly use AdapterView to obtain the underlying data without getting the reference to its Adapter. In this way, programming simplicity and decoupling are much better.
5 getItemId(int position)
Found some of its calls in AdapterView,
public long getItemIdAtPosition(int position) { T adapter = getAdapter(); return (adapter == null || position < 0) ? INVALID_ROW_ID : (position); } private void fireOnSelected() { if (mOnItemSelectedListener == null) return; int selection = (); if (selection >= 0) { View v = getSelectedView(); //The return value obtained by getItemId called here belongs to the same item's characteristics, and its significance lies in the onItemSelected method of selecting the interface // Get the id of the item directly without indirectly implementing it by obtaining the adapter (this, v, selection, getAdapter().getItemId(selection)); } else { (this); } } int findSyncPosition() { ... rowId = (seed); if (rowId == idToMatch) { // From this point of view, getItemId seems that it should return different values for different items, maintaining uniqueness // Found it! return seed; } ... }
Like the getItem() method analyzed above, getItemId() and getItemIdAtPosition() both provide programming convenience. But at present, since there is no need for id, most of the positions returned directly when rewriting the getItemId method. This is also correct, although it is meaningless to obtain data (I will give you a position, and you will return it to me intact, what does it mean). But what I want to make is that you should not be restricted by this practice, but think that ItemId is the position of the item in the data. In fact, if there is a requirement, you can use the getItemId() method to return some other values, such as the id value of each item data in the database, or each person's ID number, etc.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.