In fact, this mechanism of Android is exactly the same as that of the uitableview display method of IOS. However, after understanding this mechanism, it will be helpful for further Android learning.
Baseadapter listview is a common component in Android development. It displays data in the form of a list. Generally
Listview is composed of the following three elements: 1. View, used to display the list, usually specified by an XML. Everyone knows that android
The interface is basically completed by the XML file, so the listview interface should also use the XML definition. For example
The commonly used "android. R. layout. simple_list_item_1" in listview is an XML file defined in the Android system.
File.
2. the adapter is used to map different data to the view. Different data correspond to different adapters, such as arrayadapter, cursoradapter,
Simpleadapter, etc. They can map data such as arrays, pointers, and maps to views. It is precisely because of the adapter's
Yes, so that the use of listview is quite flexible. After processing by the adapter, it seems that all data is mapped to the same view.
. 3. Data, specific data and resources mapped, can be strings, images, etc. Through the adapter, the data will be realized
Listview. All data and resources must be displayed in the listview using an adapter.
The existing adapters of the system can display basic data to listview, such as array, data pointed by cursor, and data in map.
However, in actual development, the adapters implemented by these systems sometimes cannot meet our needs. In addition, the system comes with the Multi-choice function.
Listview has some problems in actual use. To implement a complex listview, You can inherit the listview and rewrite the corresponding
Method. You can also inherit the baseadapter. The document shows that arrayadapter, cursoradapter,
Simpleadapter inherits from baseadapter. Therefore, you can complete your own adapter by inheriting the baseadapter.
Complex combinations of data and resources are presented to you in any way you want.
After inheriting the baseadapter, you must override the following four methods: getcount, getitem, getitemid, and getview.
The process of creating a listview is as follows: first, the system will call the getcount method to obtain the number of items before creating a listview.
Each time an item is drawn, the getview method is called. In this method, the pre-defined XML can be referenced to determine the displayed
Returns a view object as an item. The main Conversion Function of the adapter is completed in this process,
Display data and resources as desired by developers. This is exactly the repeated call of getview, making listview easier to use.
And flexible. These two methods are the most important in customizing the Display Effect of listview. At the same time, as long as the two methods are overwritten, The listview will
Display as required by developers. The getitem and getitemid methods will be called when the listview response method is called.
Therefore, to ensure that the methods of listview are valid, the two methods must be rewritten. For example, the getitemid method is not completed.
When you call the getitemidatposition method of listview, you will not get the expected result, because this method is called
Getitemid method of the corresponding Adapter