This example describes the ListView usage of the Android control. Share to everyone for your reference. Specifically as follows:
Example one:
ListView is a more commonly used component in Android development, showing specific content in a list and being able to display it adaptively according to the length of the data.
Main.xml Layout file:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout android:id=
"@+id/linearlayout01"
android: Layout_width= "Fill_parent" android:layout_height= "fill_parent"
xmlns:android= "http://schemas.android.com/" Apk/res/android ">
<listview android:layout_width=" wrap_content "
android:layout_height=" Wrap_ Content "
android:id=" @+id/mylistview ">
</ListView>
</LinearLayout>
My_listitem.xml Layout file:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout "android:layout_width="
xmlns: Android= "http://schemas.android.com/apk/res/android"
android:orientation= "vertical"
android:layout_ height= "Wrap_content"
android:id= "@+id/mylistitem"
android:paddingbottom= "3dip"
android: paddingleft= "10dip" >
<textview android:layout_height= "wrap_content"
android:layout_width= "Fill_" Parent "
android:id=" @+id/itemtitle "
android:textsize=" 20dip ">
</TextView>
< TextView android:layout_height= "wrap_content" android:layout_width= fill_parent "android:id=" @+id/
Itemtext ">
</TextView>
</LinearLayout>
Lsactivity class:
Package com.ljq.ls;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.ListView;
Import Android.widget.SimpleAdapter;
public class Lsactivity extends activity {private ListView list = null;
public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
List = (ListView) Findviewbyid (R.id.mylistview);
Organization Data source list
The results of the operation are shown in the following illustration:
Example two:
Directory structure
Main.xml Layout file:
<?xml version= "1.0" encoding= "Utf-8"?>
<!--use relative layouts--> <relativelayout xmlns:android=
" Http://schemas.android.com/apk/res/android "
android:orientation=" vertical "
android:layout_width=" Wrap_ Content "
android:layout_height=" wrap_content ">
<textview android:layout_width=" 100dip
" android:layout_height= "Wrap_content"
android:layout_marginleft= "30dip"
android:textsize= "20dip"
Android:id= "@+id/id"/>
<textview android:layout_width= "100dip" android:layout_height= "
wrap_" Content "
android:layout_aligntop=" @id/id "
android:layout_torightof=" @id/id "
android:textsize=" 20dip "
android:id=" @+id/name "/>
<textview android:layout_width=" Wrap_content "
android: layout_height= "Wrap_content"
android:layout_aligntop= "@id/name"
android:layout_torightof= "@id/name"
android:textsize= "20dip"
android:id= "@+id/age"/>
</RelativeLayout>
Entity JavaBean:
Package com.ljq.domain;
public class Person {
private String ID;
private String name;
Private String age;
Public person () {
super ();
}
Public person (string ID, string name, String age) {
super ();
This.id = ID;
this.name = name;
This.age = age;
}
Public String GetId () {return
ID;
}
public void SetId (String id) {
this.id = ID;
}
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
Public String Getage () {return age
;
}
public void Setage (String age) {
this.age = age;
}
}
Custom Adapter Personadapter
Package com.ljq.ls;
Import java.util.List;
Import Com.ljq.domain.Person;
Import Android.content.Context;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.ArrayAdapter;
Import Android.widget.TextView;
/** * ListView Load Adapter Process * * 1, first determine how many data items adapter, based on this data to determine how many item * * 2, to determine the number of each item in the view * * 3, in the view to download the data to display * * * * @author Jiqinlin * */public class Personadapter extends arrayadapter{private Layoutinflater Layoutinflater =
Null
private list<person> persons; Public Personadapter (context, int textviewresourceid, List objects) {Super (context, Textviewresourceid, objec
TS);
Layoutinflater = Layoutinflater.from (context);
Persons = objects;
/** * Get how many data items in adapter/@Override public int GetCount () {return persons.size ();
@Override public Object getitem (int position) {return persons.get (position); @Override public long getitemid (int position) {return position;
/** * Create the displayed data interface * Adapter is the bridge between the ListView interface and the data, * when each item in the list is displayed to the page, the adapter GetView method is invoked to return a view. Did you ever think about it? What will it look like when we have 1000000 items in our list?
Is it taking up a lot of system resources? * * @Override public View getview (int position, View Convertview, ViewGroup parent) {///Pre Viewholder
Holder = new Viewholder ();
Convertview = layoutinflater.inflate (R.layout.main, NULL);
Holder.id = (TextView) Convertview.findviewbyid (r.id.id);
Holder.name = (TextView) Convertview.findviewbyid (r.id.name);
Holder.age = (TextView) Convertview.findviewbyid (r.id.age);
Convertview.settag (holder);
Holder.id.setText (Persons.get (position). GetId ());
Holder.name.setText (Persons.get (position). GetName ());
Holder.age.setText (Persons.get (position). Getage ());
return convertview;
*///optimized after Viewholder holder;
if (Convertview = = null) {Convertview = layoutinflater.inflate (R.layout.main, NULL);
Holder = new Viewholder ();Holder.id = (TextView) Convertview.findviewbyid (r.id.id);
Holder.name = (TextView) Convertview.findviewbyid (r.id.name);
Holder.age = (TextView) Convertview.findviewbyid (r.id.age);
Convertview.settag (holder);
}else{holder = (viewholder) convertview.gettag ();
} holder.id.setText (Persons.get (position). GetId ());
Holder.name.setText (Persons.get (position). GetName ());
Holder.age.setText (Persons.get (position). Getage ());
return convertview;
/** * Interface Display control * * @author jiqinlin * */private static class viewholder{private TextView ID;
private TextView name;
Private TextView age;
}
}
Class Lsactivity
Package com.ljq.ls;
Import java.util.ArrayList;
Import android.app.ListActivity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.ListView;
Import Android.widget.Toast;
Import Com.ljq.domain.Person;
public class Lsactivity extends Listactivity {private arraylist<person> persons = new arraylist<person> ();
Private Personadapter personadapter = null;
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
InitData ();
Personadapter =new personadapter (lsactivity.this, r.layout.main, persons);
Setlistadapter (Personadapter);
Registerforcontextmenu (Getlistview ()); @Override protected void Onlistitemclick (ListView l, View v, int position, long id) {Super.onlistitemclick (L, V
, position, id);
Person person = persons.get (position); Toast.maketext (Lsactivity.this, Person.getid () + ":" +person.getname () + ":" +person.getage (), Toast.length_long). Show
();
Return private void InitData () {Persons.add (New person ("ordinal", "name", "Age"));
Persons.add (New Person ("1", "ljq1", "20"));
Persons.add (New Person ("2", "LJQ2", "20"));
Persons.add (New Person ("3", "Ljq3", "20"));
Persons.add (New Person ("4", "Ljq4", "20"));
Persons.add (New Person ("5", "Ljq5", "20"));
Persons.add (New Person ("6", "Ljq6", "20"));
Persons.add (New Person ("7", "Ljq7", "20"));
Persons.add (New Person ("8", "Ljq8", "20"));
Persons.add (New Person ("9", "Ljq9", "20"));
}
}
Run results
I hope this article will help you with your Android program.