The example in this article describes how Android programming uses ListView to implement a data listing display. Share to everyone for your reference, specific as follows:
To display the list of data in the database on the screen, we want to use the ListView control, when the user takes out the data from the database, to bind the data to the display control, how to bind it, we need to create the adapter to bind, there are two ways to create the adapter:
The first is created with simpleadapter (requiring bound data to be list
The second type is created with simplecursoradapter (requiring bound data to be cursor data types)
The display effect is as shown in the figure:
Interface layout:
Item.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<!--item-->
<linearlayout xmlns:android=
"http ://schemas.android.com/apk/res/android "
android:orientation=" horizontal "
android:layout_width=" Fill_ Parent "
android:layout_height=" fill_parent ">
<!--name-->
<textview
android:layout_ Width= "130DP"
android:layout_height= "wrap_content"
android:id= "@+id/name"
/>
<!--telephone- ->
<textview
android:layout_width= "150DP"
android:layout_height= "Wrap_content"
Android:id= "@+id/phone"
/>
<!--deposit-->
<textview android:layout_width=
"Fill_ Parent "
android:layout_height=" wrap_content "
android:id=" @+id/amount "
/>
</ Linearlayout>
Main.xml
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout "xmlns:android=" Schemas.android.com/apk/res/android "android:orientation=" vertical "android:layout_width=" Fill_parent "Android: layout_height= "Fill_parent" > <!--title--> <linearlayout android:orientation= "Horizontal" android:layout_ Width= "Fill_parent" android:layout_height= "wrap_content" > <textview android:layout_width= "130DP" Android: layout_height= "Wrap_content" android:text= "name"/> <textview android:layout_width= "150DP" Android:layout_hei ght= "Wrap_content" android:text= "Phone"/> <textview android:layout_width= "Fill_parent" Wrap_content "android:text= deposit"/> </LinearLayout> <!--ListView control--> <listview
Dth= "Fill_parent" android:layout_height= "fill_parent" android:id= "@+id/listview"/> </LinearLayout>
data binding using Simpleadapter
public class Mainactivity extends activity {private Personservice service;
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Service = new Personservice (this);
ListView ListView = (ListView) This.findviewbyid (R.id.listview);
Gets to the collection data list<person> persons = Service.getscrolldata (0, 10);
list
data binding using Simplecursoradapter
public class Mainactivity extends activity {private Personservice service;
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Service = new Personservice (this);
ListView ListView = (ListView) This.findviewbyid (R.id.listview);
Gets the cursor Cursor Cursor = service.getcursorscrolldata (0, 10); Create the Simplecursoradapter adapter to bind the data to the item display control Simplecursoradapter adapter = new Simplecursoradapter (this, R.layout.item,
Cursor, new string[]{"name", "Phone", "Amount"}, new Int[]{r.id.name, R.id.phone, r.id.amount});
Listview.setadapter (adapter);
Entry Click event Listview.setonitemclicklistener (New Itemclicklistener ()); Private Final class Itemclicklistener implements onitemclicklistener{public void Onitemclick (adapterview<?>
Parent, view view, int position, long id) {ListView ListView = (ListView) parent; Cursor Cursor = (Cursor) listview.getitematpositIon (position);
String PersonID = string.valueof (Cursor.getint (Cursor.getcolumnindex ("_id"));
Toast.maketext (Getapplicationcontext (), PersonID, 1). Show ();
}
}
}
Note: use the second method to specify the primary key "_id" when fetching the data collection
I hope this article will help you with the Android program.