Android listview comprehension, baseadapter, and listview adapter usage and understanding

Source: Internet
Author: User
Listview is a common component in Android development. It displays data in the form of a list. Generally, a listview consists of the following three elements: 1. View, used to display the list, usually specified by an XML. As we all know, the android 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.


2. the adapter is used to map different data to the view. Different data correspond to different adapters, such as arrayadapter, cursoradapter, and simpleadapter. They can map data such as arrays, pointers, and maps to views. It is precisely because of the existence of the adapter that the use of listview is quite flexible. After the adapter is processed, it seems that all data is mapped to the same view. 3. Data, specific data and resources that do not map, can be strings, images, etc. Through the adapter, the data will be realistic to the 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's built-in 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, or inherit the baseadapter. The document shows that arrayadapter, cursoradapter, and simpleadapter all inherit from baseadapter. Therefore, by inheriting the baseadapter, you can complete your own adapter and display any complicated combination of data and resources with any desired display effect.


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 the 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 display effect and a view object is returned as an item. In this process, the main Conversion Function of the adapter is completed to display data and resources as desired by developers. It is also the repeated call of getview that makes the use of listview simpler and more 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 rewritten, listview can be fully displayed as required by developers.


The getitem and getitemid methods will be called when the listview response method is called. To ensure that the methods of listview are valid, you must rewrite these two methods. For example, if the implementation of the getitemid method is not completed, the expected result will not be obtained when the getitemidatposition method of listview is called, because the method calls the getitemid method of the corresponding adapter.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.