Android Common UI Components-ListView

Source: Internet
Author: User

List view (ListView) is a very important component of Android, it shows the content in the form of a list, and can be displayed adaptively according to the length of the data.

The display of a list requires three elements:

1. Listveiw The view used to display the list.

2. The adapter is used to map data to mediations on the ListView.

3. The data is specific to the string, picture, or base component that will be mapped.

First, the basic concept of "adapter" is introduced. The data defined in the list is mapped to the ListView through "adapters", and there are two commonly used adapters in the ListView:

Arrayadapter: The simplest adapter, can only display one line of text;

Simpleadapter: A well-extensible adapter that can display custom content.

Display with Arrayadapter combined with a ListView

Run effect

Source Code Activitylist1.java

Package Com.rainsong.listviewdemo;import Java.util.list;import Java.util.arraylist;import android.app.Activity; Import Android.os.bundle;import Android.view.view;import Android.widget.AdapterView;  Import Android.widget.AdapterView.OnItemClickListener; Import Android.widget.arrayadapter;import Android.widget.listview;import Android.widget.toast;public class    ActivityList1 extends activity{listview listview;    List<string> data;    Arrayadapter Arrayadapter;    Onitemclicklistener Listener; /** called when the activity is first created.        */@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        ListView = New ListView (this);        data = new arraylist<string> ();        Data.add ("Item1");        Data.add ("Item2");        Data.add ("Item3"); Arrayadapter = new Arrayadapter (this, Android.        R.layout.simple_list_item_1, data);        Listview.setadapter (Arrayadapter); Listener = new OnItemclicklistener () {public void Onitemclick (adapterview<?> parent, view view, int PO Sition, long id) {Toast.maketext (activitylist1.this, parent.getitematposition (position). toString () + "CLI            Cked ", Toast.length_short). Show ();        }        };        Listview.setonitemclicklistener (listener);    Setcontentview (ListView); }}


API Knowledge Points

Arrayadapter

public class

Arrayadapter

Extends Baseadapter

Implements Filterable


Arrayadapter (context context, int resource, list<t> objects)

Constructor


Listview

public class

Listview

Extends Abslistview

ListView (Context context)


void

Setadapter (ListAdapter adapter)

Sets thedata behind this ListView.

void

Setonitemclicklistener (Adapterview.onitemclicklistener Listener)

Register Acallback to is invoked when a item in this adapterview have been clicked.


Adapterview.onitemclicklistener

Public Static Interface

Adapterview.onitemclicklistener


abstract void

Onitemclick (adapterview<?>parent, view view, int position, long ID)

Callbackmethod to is invoked when a item in this adapterview have been clicked.


Adapterview

Public abstract class

Adapterview

Extends ViewGroup

Object

getitematposition (int position)

Gets the data associated with the specified position in the list.

Toast

public class

Toast

Extends Object

Constants

int Length_long showthe View or text notification for a LONG period of time.

int Length_short showthe View or text notification for a short period of time.

Static Toast

Maketext (context context, int resId, int duration)

Make a standard toast, the just contains a text view with the text from a resource.

Static Toast

Maketext (Context context, charsequence text, int duration)

Make a standard toast, that just contains a text view.

void

Show ()

Show The View for the specified duration.

Display with Simpleadapter combined with a ListView

Run effect

Source Code activitylist 2 . Java

Package Com.rainsong.listviewdemo;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.view;import Android.widget.AdapterView;  Import Android.widget.AdapterView.OnItemClickListener; Import Android.widget.listview;import Android.widget.simpleadapter;import Android.widget.toast;public class    ActivityList2 extends activity{private list<map<string, object>> data;    Private ListView ListView;    Private Simpleadapter adapter;    Onitemclicklistener Listener; /** called when the activity is first created.        */@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        data = new arraylist<map<string, object>> ();        Map<string, object> item;        item = new hashmap<string, object> ();        Item.put ("name", "Zhang San");        Item.put ("Gender", "male");        Data.add (item); Item = new hashmap<string, object> ();        Item.put ("name", "John Doe");        Item.put ("Gender", "male");        Data.add (item);        item = new hashmap<string, object> ();        Item.put ("name", "Harry");        Item.put ("Gender", "male");        Data.add (item);        item = new hashmap<string, object> ();        Item.put ("name", "Snow");        Item.put ("Gender", "female");        Data.add (item);        item = new hashmap<string, object> ();        Item.put ("name", "Xiaoming");        Item.put ("Gender", "male");        Data.add (item);        ListView = New ListView (this); adapter = new Simpleadapter (this, data, Android. R.layout.simple_list_item_2, new string[] {"Name", "Gender"}, new int[] {Android. R.id.text1, Android.         R.ID.TEXT2});         Listview.setadapter (adapter);         Setcontentview (ListView);                    Listener = new Onitemclicklistener () {public void Onitemclick (adapterview<?> parent, View view, int position, long ID) {toAst.maketext (Activitylist2.this, parent.getitematposition (position). toString () + "clicked", Toast.lengt            H_short). Show ();        }        };    Listview.setonitemclicklistener (listener); }}

API Knowledge Points

Simpleadapter

public class

Simpleadapter

Extends Baseadapter

Implements Filterable

Simpleadapter (Contextcontext, list<? extends Map<string,?>> data, int resource,string[] from, int[] to)

Constructor

Android Common UI Components-ListView

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.