Android note 11. ListView + Adapter

Source: Internet
Author: User

Android note 11. ListView + Adapter
Understanding Adapter 1. ListViewListView 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: Displays the list view, which is usually specified by an xml file. 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. Adapter: Maps different data to the View. Different data correspond to different adapters, such as ArrayAdapter, CursorAdapter, and SimpleAdapter. They can Map data or basic components 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. It can be a string image, Map ing, etc. Using the adapter, the data will be real to the ListView. In short, all the data and resources should be displayed to the ListView through the adapter. ArrayAdapter, CursorAdapter, and SimpleAdapter are the existing adapters of the system. They can Map data in arrays and Cursor (usually databases.
2. Understanding Adapter in Depth 1. Summary: Adapter is an Adapter interface connecting back-end data and front-end View display. It is a bridge between data and UI (View. By using the adapter, You can map different data and display it on The View. Adapter is required in common View (ListView, GridView) and other places. Shows the relationship between Data, Adapter, and View:2. Adapter class in Android (1) BaseAdapter: Is an abstract class that can be used to implement more complex ListView. 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 and process 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. However, ArrayAdapter, CursorAdapter, and SimpleAdapter do not have to overwrite the above four methods.(2) ArrayAdapter Adapter: Supports generic operations, which is the simplest. Only one line of strings can be displayed.(3) SimpleAdapter: Has the best scalability, Can Customize various effects (string, image, Map set), usually use xml layout file to specify the view.(4) SimpleCursorAdapter Adapter: It can be used for simple text-only ListView. It requires the Cursor field to correspond to the UI id. You can rewrite other methods to implement a more complex UI. SimpleAdapter can be considered as a simple combination of databases, which can easily display the database content in the form of a list.
3. The general process of ListView loading the adapter(1) first, determine the number of data items in the adapter and then determine the number of items (list items) based on the data ). ----- getCount () method implementation (2) determine which View is loaded in each item. ----- getView () method implementation (3) load the data to be displayed to the corresponding View4. ArrayAdapter instance analysis (1) Basic development ideasThe ArrayAdapter adapter supports generic operations. Each item in the list can only display a string of characters, but cannot display resource data such as images. * Use a string array as the data source a. Implement a string array as the data source (List set) (there are several array elements, there are several List items) B. instantiate an ArrayAdapter Adapter object, used to assemble the data source to the specified View. ArrayAdapter Adapter = new ArrayAdapter (This, android. R. layout. simple_expandable_list_item_1, strs); c. Apply the ArrayAdapter to the ListActivity ListView setListAdapter (adapter );
* Use the List set (ArrayList) as the data source a. instantiate a ListView object B. instantiate an ArrayAdapter Object (the data item type in the set is String), which maps the data items in the Map set to the list items. ArrayAdapter Adapter = new ArrayAdapter (This, android. r. layout. simple_expandable_list_item_1, getData (); c. assemble the Adapter to the ListView and display the ListView view. setAdapter (adapter); // assemble the Adapter to the ListView setContentView (listview); // display the list View (2) source code analysis 1 MainActivt. java package com. example. android_arraylist_1; mport android. app. listActivity; import android. OS. bundle; import android. widget. arrayAdapter; public class MainActivity extends ListActivity {public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); String [] strs = {item 1, Item 2, Item 3, item 4}; // list item data ArrayAdapter Adapter = new ArrayAdapter (// Instantiate an ArrayAdapter adapter to assemble data this, // context android. r. layout. simple_expandable_list_item_1, // The layout of each row in the List, showing only one line of text strs); // data source (a List set) setListAdapter (adapter ); // display List set data in List View} analysis: the above Code uses ArrayAdapter (Context context, int textViewResourceId, List Objects) to assemble the data. to assemble the data, you need an adapter connecting the ListView view object and the array data to adapt the two. The ArrayAdapter structure requires three parameters, which are this in turn, layout file (note that the layout file here describes the layout of each row in the list, android. r. layout. simple_list_item_1 is a layout file defined by the system that only displays one line of text, a data source (a List set ). At the same time, use setAdapter () to complete the final work of adaptation. Effect: (3) Source Code Analysis 2. ArrayListActivity. java
Package com. example. android_arraylist_2; import java. util. arrayList; import java. util. list; import android. app. activity; import android. OS. bundle; import android. widget. arrayAdapter; import android. widget. listView; public class ArrayListActivity extends Activity {private ListView listview; // defines a ListView object @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); listview = new ListView (this); // instantiate a ListView object ArrayAdapter Adapter = new ArrayAdapter (This, // instantiate an adapter android. r. layout. simple_expandable_list_item_1, // layout file: display a line of text getData (); // obtain the data source (ArrayList set) listview. setAdapter (adapter); // assemble the Adapter to the ListView setContentView (listview); // display the list View} private List GetData () {// TODO Auto-generated method stub List Data = new ArrayList (); // Instantiate an ArrayList set for loading data. add (test data 1); // add element data to the ArrayList set. add (Test data 2); data. add (test data 3); data. add (test data 4); return data; // return ArrayList set object} analysis: (1) getData () (2) List (3) List Data = new ArrayList () Effect:
5. SimpleAdapter instance analysisUnlike ArrayAdapter, simpleAdapter has the best scalability. It can be used to define various la s, including ImageView, Button, and CheckBox. Generally, we use the Map hash table as the data source and Map the key to the component Id to implement the list items. (1) Basic development ideasA. assemble the adapter and Map the Map key data resource to the component corresponding to the Id. SimpleAdapter adapter = new SimpleAdapter (this, // context getData (), // obtain the list dataset R. layout. simple, // specify the layout file new String [] {img, title1, info}, // Map set key new int [] {R. id. img, R. id. title1, R.id.info}); // interface component Id B. implement a Map set and add the map object to the List in ArrayList > List = new ArrayList > (); // Instantiate an ArrayList object, Map Map = new HashMap (); // Set the first title and icon of the List (Use Map) c. Apply the ArrayAdapter to the ListActivity ListView setListAdapter (adapter );
(2) Source Code AnalysisSimpleAdapterActivity. java
Package com. example. android_sampleadapter_1; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import android. app. listActivity; import android. OS. bundle; import android. widget. simpleAdapter; public class SimpleAdapterActivity extends ListActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // 1. assemble the adapter and bind the data to the corresponding view SimpleAdapter adapter = new SimpleAdapter (this, // context getData (), // obtain the list dataset R. layout. simple, // specify the layout file new String [] {img, title1, info}, // Map set key new int [] {R. id. img, R. id. title1, R.id.info}); // interface component Id // 2. display view setListAdapter (adapter);} // 3. map Dataset: A List composed of HashMap. Each section of the list corresponds to each row of ListView. // each key value of HashMap is mapped to the private List of the component corresponding to the id in the layout file. > GetData () {// TODO Auto-generated method stub List > List = new ArrayList > (); // Instantiate an ArrayList object, Map Map = new HashMap (); // Set the first title and icon of the List (using Map) map. put (img, R. drawable. a); map. put (title1, Huawei honor); map. put (info, learn how to buy, really TM pitfall !); List. add (map); // add a map object to the list map = new HashMap (); Map. put (img, R. drawable. b); map. put (title1, China Mobile); map. put (info, god knows); list. add (map); // add a map object to the list map = new HashMap (); Map. put (img, R. drawable. c); map. put (title1, ZTE); map. put (info, mobile phone is not bad, just dying); list. add (map); // add a map object to the list map = new HashMap (); Map. put (img, R. drawable. d); map. put (title1, meizu 4); map. put (info, a little expensive !); List. add (map); // add a map object to the return list;} analysis:
Data Using simpleAdapter is generally a List composed of HashMap. Each section of the list corresponds to each row of the ListView. Each key-value data of HashMap is mapped to the component corresponding to the id in the layout file. Because the system does not have a corresponding layout file available, we can define a layout simple. xml. The following is an adaptation. The new SimpleAdapter parameter is: this, layout file (vlist. xml), HashMap title and info, img, layout file component id, title, info, img. The components of the layout file are mapped to the elements of HashMap to complete adaptation. Simple. xml-list item layout File Xmlns: tools = http://schemas.android.com/tools
Android: orientation = horizontal
Android: layout_width = match_parent
Android: layout_height = match_parent>

Android: id = @ + id/img
Android: layout_width = wrap_content
Android: layout_height = wrap_content
Android: layout_margin = 10dp/>

Android: id = @ + id/title1
Android: layout_width = wrap_content
Android: layout_height = wrap_content
Android: textColor = # FF000000 android: textSize = 22sp/>
Android: id = @ + id/info
Android: layout_width = wrap_content
Android: layout_height = wrap_content
Android: textColor = # FF000000 android: textSize = 13sp/>



6. SimpleAdapter instance analysis
Package com. example. android_simplecuroradapter; import android. app. listActivity; import android. database. cursor; import android. OS. bundle; import android. provider. contacts. people; import android. support. v4.widget. simpleCursorAdapter; import android. widget. listAdapter; public class simplecursoracti1_extends ListActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // 1. obtain a CurSor object pointing to the system Address Book database to obtain the data source Cursor cur = getContentResolver (). query (People. CONTENT_URI, null, null); startManagingCursor (cur); // 2. instantiate list adapter ListAdapter adapter = new SimpleCursorAdapter (this, android. r. layout. simple_expandable_list_item_1, cur, new String [] {People. NAME}, new int [] {android. r. id. text1}); // 3. display view setListAdapter (adapter);} analysis: SimpleCursorAdapter can be used only when the database is used as the data source. Note that you should not forget it in AndroidManifest. add permissions to xml files Effect:



Related Article

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.