First, create a layout that displays an item named Item.xml
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Horizontal" >
<textview
Android:id= "@+id/name"
Android:layout_width= "120DP"
android:layout_height= "Wrap_content"/>
<textview
Android:id= "@+id/phone"
Android:layout_width= "150DP"
android:layout_height= "Wrap_content"/>
<textview
Android:id= "@+id/amount"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"/>
</LinearLayout>
Then, in Main.xml, add a ListView data display control, adding an ID name called ListView
The next step is to bind the data into three different ways:
Copy Code code as follows:
private void
Show1() {
List persons = Ps.getscolldata (0, 10);
listfor (person Person:persons) {
hashmap<string, object> item = new hashmap<string, object> ();
Item.put ("Amount", Person.getamount ());
Item.put ("id", Person.getid ());
Item.put ("Name", Person.getname ());
Item.put ("Phone", Person.getphone ());
Data.add (item);
}
Simpleadapter adapter = new Simpleadapter (Getapplicationcontext (), Data, R.layout.item,
New string[] {"Name", "Phone", "Amount"}, new int[] {r.id.name, r.id.phone, r.id.amount});
Item is represented as a previously defined item.xml, which means that each object in the data collection is bound to some item, and each item in data is bound to a view item.
The latter two parameters represent the values of which keys in the result set are bound to which controls, (binding the object in the result set key to name to a control with ID name in the view)
Listview.setadapter (adapter);//internal processing flow as follows
{int total = Adapter.getcount ();//Get all the data
int perpage = 7;//Gets the number of displayed entries for each page,
for (int i = 0; i < perpage; i++) {
View view = Adapter.getview (I, convertview, parent);//The second execution will pass the view of the previous execution to Convertview;
Show entries
// }}
}
private void Show2 () {//This method requires a cursor in the result set, requiring a field called _id in the cursor, so add a method to get the cursor as follows!
Cursor Cursor = ps.getcursorscolldata (0, 10);
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);
}
Public Cursor getcursorscolldata (int offest, int maxresult) {
Sqlitedatabase db = Dbopenhelper.getreadabledatabase ();//Because the result set requires a word named _id, the SQL statement is as follows!
Cursor Cursor = Db.rawquery ("Select PersonID as _id,name,phone,amount from person order by PersonID ASC limit?,?",
New string[] {string.valueof (offest), string.valueof (Maxresult)});
Db.query (table, columns, selection, Selectionargs, GroupBy, Having,orderby, limit);
return cursor;
}
private void show3 () {
List persons = ps.getscolldata (0);//Personadapter See next Chapter Custom Adapter
Personadapter adapter = new Personadapter (Getapplicationcontext (), persons, R.layout.item);
Listview.setadapter (adapter);
}
Add this method when you need to get information about this item for each click:
Listview. Setonitemclicklistener(New Itemclicklistener ());
Private Final class Itemclicklistener implements Onitemclicklistener {
@Override//The first parameter in this method is the control that displays the data (item item), in this case ListView, and the third parameter is the location of the clicked item.
public void Onitemclick (adapterview<?> parent, View arg1, int position,long arg3) {
ListView LView = (ListView) parent;
The corresponding processing method of SHOW3 () method
person who = (person) lview.getitematposition (position);
Toast.maketext (Getapplicationcontext (), Person.getid (). toString (), 1). Show ();
The corresponding processing method of Show2 () method
In the Show2 method, adapter returns cursor,
Cursor Cursor = (Cursor) lview.getitematposition (position);
int PersonID = Cursor.getint (Cursor.getcolumnindex ("_id"));
Toast.maketext (Getapplicationcontext (), PersonID + "", 1). Show ();
The corresponding processing method of Show1 () method
In the Show1 method, the adapter returns the map, and then the map is manipulated to remove the corresponding ID value
hashmap<string, object> item = (hashmap<string, object>) lview.getitematposition (position);
int PersonID = (Integer) item.get ("id");
Toast.maketext (Getapplicationcontext (), PersonID + "", 1). Show ();
}
}