首先,创建一个用于显示一个item的layout,名为item.xml
android:layout_
android:layout_ />
android:layout_
android:layout_ />
android:layout_
android:layout_ />
接下来便是进行数据的绑定,总共分为三种方法:
private void show2() {//此方法需要一个结果集中的Cursor,要求Cursor中需要有一个名称为_id的字段;所以添加一个获取Cursor的方法如下!
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();//因为要求结果集中要有一个名为_id的字,所以SQL语句如下!
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, 10);// PersonAdapter见下一章节自定义适配器
PersonAdapter adapter = new PersonAdapter(getApplicationContext(),persons, R.layout.item);
listview.setAdapter(adapter);
}
private final class ItemClickListener implements OnItemClickListener {
@Override//此方法中第一个参数为显示数据(item项)的控件,在此例子中即为ListView;第三个参数为点击项的位置,
public void onItemClick(AdapterView> parent, View arg1, int position,long arg3) {
ListView lview = (ListView) parent;
// show3()方法对应的处理方法
// Person person = (Person) lview.getItemAtPosition(position);
// Toast.makeText(getApplicationContext(),person.getId().toString(),1).show();
// show2()方法对应的处理方法
// show2方法中,adapter返回的是Cursor,
// Cursor cursor = (Cursor) lview.getItemAtPosition(position);
// int persOnid= cursor.getInt(cursor.getColumnIndex("_id"));
// Toast.makeText(getApplicationContext(), personid + "", 1).show();
// show1()方法对应的处理方法
// show1方法中,adapter返回的是Map,再对Map进行操作取出相应的id值
HashMap
int persOnid= (Integer) item.get("id");
Toast.makeText(getApplicationContext(), personid + "", 1).show();
}
}