Android development details of the ListView in a detailed

Source: Internet
Author: User

List ListView Introduction and examples


1.ListView--listactivity--ListAdapter
2.ArrayAdapter combined with ListView for display
3.SimpleAdapter combined with ListView for display
4. Handle clicking the ListView event to handle the selection of the ListView event
5. Associating with a ListView using Simplecursoradapter

Listview:

To have a ListView show the 3 conditions required:

1.ListView---The list that needs to be displayed
2.Data---and listview-bound data, typically a cursor or array of strings
3.ListAdapter---Bridge---Adaptation of data and ListView


Common ListAdapter Subclasses:


Arrayadapter---Arrays----direct subclasses of a listadapter


arraylist< type > (context,int itemlayout, data)
Context: Contextual Environment
ItemLayout: Customize each of the layout to be displayed
Data: What to display
Example:
arrayadapter<string> adapter = new Arrayadapter<string> (Arrayadapterlistviewactivity.this, Android. R.layout.simple_list_item_1, data);
Android. R.layout.simple_list_item_1---Use every listview display layout that comes with Android--this item layout is often customized in real-world applications


Special NOTE: ItemLayout---The layout of each item (row) in the listview---can refer to the system's own, but more of the actual project is using a custom item layout


The system comes with a common item layout:
Android. R.layout.simple_list_item_1-----Only one TextView per item
Android. R.layout.simple_list_item_1-----Only two TextView per item
Android. R.layout.simple_list_item_single_choice-----Each item has a textview, but this one can be selected


Simpleadapter---Simple adaptation----the direct sub-class of a listadapter

With Simpleadapter, you can make every item in the ListView more personal,
The layout information of an item in the ListView is usually written in an XML file, which is introduced into the ListView by r.layout.xx-

Simpleadapter is the bridge between the ArrayList and the ListView.
Each item in the ArrayList is a map<string,?> type. Each map object in the ArrayList is data-bound and one by one-compatible with one of the ListView


Simpleadapter's constructor:
Public Simpleadapterk (context context, list<?. extends map<string,?>> data, int resource, string[] from, int[] To)


Context--contexts
Data--Each item in map-based list,data corresponds to every item in the Listiview. Every item in data is a map type,
The Map class contains the data required for each row of the listview---commonly-used data = new arraylist<map<string,object>> ();
Resource-a layout that must contain the view that appears in to, either a system-supplied or a custom layout layout.
The system provides: Android. R.layout.simpe_list_item_single_choice
Android. R.layout.two_line_list_item
From--array name, each name is to index map<string in each item in ArrayList, Object> Object with the
To--this is an array of TextView, these textview are represented as IDs, and the shoe view must be TextView



Add event handling for each item in the ListView: Onitemclicklistener

Onitemclicklistener listener = new Onitemclicklistener () {


@Override
public void Onitemclick (adapterview<?> parent, view view, int position,
Long id) {
Settitle (parent.getitematposition (position). ToString ());
}
};

Listview.setonitemclicklistener (listener);


Description
Onitemclick-callback function that automatically executes when the user clicks each item in the ListView
The parent---a ListView that was clicked
View---The item that the user is working on
Position the position of the item item in the ListView---operation
ID---The ID of the row that was selected


Getitematposition (position)---get the data bound to this line by position, in this case just write the row content as title

Listview.setonitemclicklistener (Listener)---Register the Listener in the ListView


Instance code:

Arrayadapter and the ListView


Package Com.example.androidbasicdemo1;import Android.app.activity;import Android.os.bundle;import Android.view.menu;import Android.view.menuitem;import Android.widget.arrayadapter;import Android.widget.ListView  ;/** * arraylist< type > (context,int itemlayout, data) * Context: Contexts * itemlayout: Customize each item to display the layout * Data: Data to display * * Example: * arrayadapter<string> adapter = new Arrayadapter<string> (Arrayadapterlistviewactivity.this, Android.   R.layout.simple_list_item_1, data); Android. R.layout.simple_list_item_1---Use every listview display layout that comes with Android-often customizing this item layout in real-world applications * @author Jayhe * */public class Arra Yadapterlistviewactivity extends Activity {private ListView listview; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_array_adapter_list_ view);//Get ListView object from XML layout file ListView = (ListView) Findviewbyid (R.ID.ARRAYADAPTERLISTVIEW1);//define the data to be displayed string[] data = Getresources (). Getstringarray (R.array.items_country);//define Adapter Arrayadapter<string> adapter = new Arrayadapter<string> (arrayadapterlistviewactivity.this Android. R.layout.simple_list_item_1, data);//Add Adapter Listview.setadapter (adapter) for the ListView;} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.array_adapter_list_view, menu); return true;} @Overridepublic boolean onoptionsitemselected (MenuItem item) {int id = item.getitemid (); if (id = = r.id.action_settings) { return true;} return super.onoptionsitemselected (item);}}



Simpleadapter and the ListView


Package Com.example.androidbasicdemo1;import Java.util.arraylist;import Java.util.hashmap;import java.util.List; Import Java.util.map;import android.app.activity;import Android.os.bundle;import Android.view.menu;import Android.view.menuitem;import Android.view.view;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.listview;import Android.widget.simpleadapter;public class Simpleadapterlistviewactivity extends Activity {private list<map< String, Object>> data;private listview listview, @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_simple_adapter_list_view); ListView = ( ListView) Findviewbyid (R.ID.SIMPLEADAPTERLISTVIEW1); Simpleadapter adapter = new Simpleadapter (Simpleadapterlistviewactivity.this, Preparedata (), R.layout.slef_list_ Item1,new string[]{"name", "Gender"}, New Int[]{r.id.selflistview1textview1,r.id.selflistview1textview2}); ListView.setAdapter (Adapter); Onitemclicklistener listener = new Onitemclicklistener () {@Overridepublic void Onitemclick ( Adapterview<?> Parent, view view, int Position,long id) {settitle (parent.getitematposition (position). ToString () );}}; Listview.setonitemclicklistener (listener);} Private list<map<string, object>> Preparedata () {data = new arraylist<map<string, object>> (); map<string, object> item;item = new hashmap<string, object> (), Item.put ("name", "Wu Huan small Pot Friend"), Item.put ("Gender", "female"); Data.add (item), item = new hashmap<string, object> (), Item.put ("name", "Feng Rong Child"), Item.put ("Gender", "female");d Ata.add (item) ; item = new hashmap<string, object> (); Item.put ("name", "Xia Ming"), Item.put ("Gender", "male");d Ata.add (item); item = new hashmap& Lt String, object> (); Item.put ("Name", "Xiao Hua"), Item.put ("Gender", "male");d Ata.add (item); return data;} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.simple_adapter_list_view, menu); return true;} @Overridepublic Boolean OnoptioNsitemselected (MenuItem item) {int id = item.getitemid (); if (id = = r.id.action_settings) {return true;} return super.onoptionsitemselected (item);}}


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.