Getting started with Android: listview

Source: Internet
Author: User
1. Introduction to listview

 

Listview is similar to dividing an interface into one line, for example:

 

Listview is only a view. To display data in this list view, you must use "adapter". We usually have three types of adapters:

(1) arrayadapter: each line can only be one text;

(2) simpleadapter: This method is not very good. After testing, only one row of data can be displayed;

(3) inherit baseadapter: This method is flexible and can be customized to correspond each line to different layout XML;

(4) simplecursoradapter: the incoming data is not a list, but a cursor returned by the database query;

 

Ii. listview instance

 

This instance implements the use of three listview types;

The project hierarchy is as follows:

 

Listview interface:

 

The arrayadapter display interface is as follows:

 

The simpleadapter interface is as follows:

 

The extendbaseadapter interface is as follows:

 

 

1. Main interface module:

 

Listviewactivity. Java

 

Package Org. xiazdong; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. arrayadapter; import android. widget. button; public class listviewactivity extends activity implements onclicklistener {private button btn1; private button btn2; private button btn3; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // you must first setcontentview before findviewbyid btn1 = (button) This. findviewbyid (R. id. arrayadapter); btn2 = (button) This. findviewbyid (R. id. simpleadapter); btn3 = (button) This. findviewbyid (R. id. baseadapter); btn1.setonclicklistener (this); btn2.setonclicklistener (this); btn3.setonclicklistener (this);} @ overridepublic void onclick (view v) {If (V = btn1) {intent = new intent (listviewactivity. this, arrayadapteractivity. class); this. startactivity (intent);} else if (V = btn2) {intent = new intent (listviewactivity. this, simpleadapteractivity. class); this. startactivity (intent);} else if (V = btn3) {intent = new intent (listviewactivity. this, extendbaseadapteractivity. class); this. startactivity (intent );}}}

Main. xml

 

<? XML version = "1.0" encoding = "UTF-8"?> <Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <textview Android: id = "@ + ID/TV" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: layout_centerhorizontal = "true" Android: TEXT = "listview usage" Android: textsize = "50px"/> <button Android: Id = "@ + ID/arrayadapter" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: layout_below = "@ ID/TV" Android: text = "arrayadapter"> </button> <button Android: id = "@ + ID/simpleadapter" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: layout_below = "@ ID/arrayadapter" Android: TEXT = "simpleadapter"> </button> <button Android: Id = "@ + ID/baseadapter" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: layout_below = "@ ID/simpleadapter" Android: text = "extendbaseadapter"> </button> <textview Android: Id = "@ + ID/textview1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Rule = "true" Android: layout_alignparentright = "true" Android: layout_marginbottom = "142dp" Android: layout_marginright = "16dp" Android: text = "by xiazdong" Android: textsize = "20px"/> </relativelayout>

 

2. arrayadapter Module

 

Arrayadapteractivity. Java

 

Package Org. xiazdong; import Java. util. arraylist; import Java. util. list; import android. app. activity; import android. OS. bundle; import android. widget. arrayadapter; import android. widget. listview;/** each time an activity is created, it must be stored in androidmanifest. * arrayadapter can only display basic strings * simpleadapter can display custom views */public class arrayadapteractivity extends activity {private listview lv; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); Lv = new listview (this); // listview inherits viewlist <string> List = new arraylist <string> (); For (INT I = 0; I <10; I ++) {list. add ("xiazdong-" + I);} arrayadapter <string> SA = new arrayadapter <string> (this, // assign list to arrayadapter and use simple_item_1 style android. r. layout. simple_list_item_1, list); LV. setadapter (SA); // map the value of arrayadapter to listview this. setcontentview (LV );}}

 

3. simpleadapter Module

 

Generally, listview is used to display the list. Generally, the data in the list comes from the database. Therefore, we assume that a dbservice class has been implemented before, and pagequery (INT offset, int length) exists in it );

For example, dbservice. pagequery (3, 5); indicates that three records are skipped and five records are inserted;

Main. xml


<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <linearlayout Android: layout_width = "wrap_content" // note that wrap_content Android: layout_height = "wrap_content" // note that wrap_content Android: Orientation = "horizontal"> <textview Android: layout_width = "100dp" Android: layout_height = "wrap_content" Android: text = "ID"/> <textview Android: layout_width = "100dp" Android: layout_height = "wrap_content" Android: TEXT = "name"/> <textview Android: layout_width = "100dp" Android: layout_height = "wrap_content" Android: text = "Age"/> </linearlayout> <listview Android: id = "@ + ID/listview" Android: layout_width = "wrap_content" Android: layout_height = "fill_parent"> </listview> </linearlayout>

Item. xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="horizontal" >    <TextView        android:id="@+id/id1"        android:layout_width="100dp"        android:layout_height="wrap_content"        />    <TextView        android:id="@+id/name"        android:layout_width="100dp"        android:layout_height="wrap_content"        />    <TextView        android:id="@+id/age"        android:layout_width="100dp"        android:layout_height="wrap_content"        /></LinearLayout>

Mainactivity. Java

Package Org. xiazdong. DB; import Java. util. arraylist; import Java. util. hashmap; import Java. util. list; import Java. util. map; import Org. xiazdong. DB. domain. person; import Org. xiazdong. DB. service. dbservice; import android. app. activity; import android. OS. bundle; import android. util. log; import android. widget. listview; import android. widget. simpleadapter; public class mainactivity extends activity {private listview; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); listview = (listview) This. findviewbyid (R. id. listview); dbservice service = new dbservice (this); List <person> Persons = service. pagequery (0, 10); List 

Conclusion: simpleadapter does not need to implement the adapter internally. It can only implement a list with the same layout for each item;

 

4. inherit the baseadapter class

 

Extendbaseadapteractivity. Java

 

Package Org. xiazdong; 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 extendbaseadapteractivity extends activity {private listview lv; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); Lv = new listview (this); myadapter MA = new myadapter (this, new int [] {R. layout. sub1, R. layout. sub2}); // display the layout of sub1 and the layout of sub2 in each row. setadapter (MA); this. setcontentview (LV );}}

 

Myadapter. Java

 

Package Org. xiazdong; import android. content. context; import android. view. layoutinflater; import android. view. view; import android. view. view. onclicklistener; import android. view. viewgroup; import android. widget. baseadapter; import android. widget. button; import android. widget. checkbox; import android. widget. compoundbutton; import android. widget. compoundbutton. oncheckedchangelistener; import android. widget. toast; public class myadapter extends baseadapter {private context; private int layoutid []; private layoutinflater inflator; Public myadapter (context, int layoutid []) {This. context = context; this. layoutid = layoutid; inflator = layoutinflater. from (context) ;}@ overridepublic int getcount () {// a layout occupies a row return layoutid. length ;}@ overridepublic object getitem (INT position) {return NULL ;}@ overridepublic long getitemid (INT position) {return 0 ;} @ override // position indicates the row to be displayed, and 0 indicates the first row. // convertview indicates the public view getview (INT position, view convertview, viewgroup parent) to be displayed) {convertview = inflator. inflate (layoutid [position], null); // map a layout XML to one if (convertview. findviewbyid (R. id. checkbox1) instanceof checkbox) {checkbox CB1 = (checkbox) convertview. findviewbyid (R. id. checkbox1); cb1.setoncheckedchangelistener (New oncheckedchangelistener () {@ overridepublic void oncheckedchanged (compoundbutton buttonview, Boolean ischecked) {toast T = toast. maketext (context, "click the check box", toast. length_short); T. show () ;}}) ;}if (convertview. findviewbyid (R. id. button1) instanceof button) {button b1 = (button) convertview. findviewbyid (R. id. button1); b1.setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {toast T = toast. maketext (context, "click", toast. length_short); T. show () ;}}) ;}return convertview ;}}

 

Sub1.xml

 

<? XML version = "1.0" encoding = "UTF-8"?> <Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "match_parent" Android: layout_height = "match_parent"> <textview Android: Id = "@ + ID/textview1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_alignbaseline = "@ + ID/checkbox1" Android: layout_alignbottom = "@ + ID/checkbox1" Android: layout_alignparentleft = "true" Android: text = "click to bring up the prompt"/> <checkbox Android: Id = "@ + ID/checkbox1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_alignparentright = "true" Android: layout_alignparenttop = "true" Android: text = ""/> </relativelayout>

 

Sub2.xml

 

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" > <TextView     android:id="@+id/textView2"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignBaseline="@+id/button1"     android:layout_alignBottom="@+id/button1"     android:layout_alignParentLeft="true"     android:text="TextView" /><Button     android:id="@+id/button1"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignParentRight="true"     android:layout_alignParentTop="true"     android:text="Button" /></RelativeLayout>

5. simplecursoradapter Note: To use simplecursoradapter, the primary key of the result set of cursor is required to be named _ id. Therefore, we must use as _ id for alias during query;
For example, select ID, name from person; select ID as _ id, name from person;

Main steps:

Cursor cursor = query ();

Simplecursoradapter adapter = new simplecursoradapter (this,/* cotnext */

R. layout. Item,/* layout of an item */

Cursor,

New String [] {"name", "Age"},/* name, age are the attributes of tables in the database */

New int [] {R. Id. Name, R. Id. Age});/* R. Id. Name, R. Id. Age can display data */

Listview. setadapter (adapter );

Summary:

 

There are many listview applications, such

(1) List rankings in general games;

(2) Multiple seed settings are listed in "Settings;

(3) list words in the dictionary;

 

 

 

(4) Address Book list;

 

 

 

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.