欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android ListView數(shù)據(jù)綁定顯示的三種解決方法

 更新時(shí)間:2013年04月21日 14:21:14   作者:  
本篇文章小編為大家介紹,Android ListView數(shù)據(jù)綁定顯示的三種解決方法。需要的朋友參考下

首先,創(chuàng)建一個(gè)用于顯示一個(gè)item的layout,名為item.xml

復(fù)制代碼 代碼如下:

<?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>


然后,在main.xml中,添加一個(gè)ListView數(shù)據(jù)顯示控件,添加id名稱為listview

接下來便是進(jìn)行數(shù)據(jù)的綁定,總共分為三種方法:

復(fù)制代碼 代碼如下:

 private void show1() {
  List persons = ps.getScollData(0, 10);
  List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
  for (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表示為之前定義的item.xml,表示將data集合中的每一個(gè)對(duì)象綁定到些item中,即將data中的每一項(xiàng)綁定到一個(gè)視圖item上;
          // 后兩個(gè)參數(shù)表示把結(jié)果集中哪些key的值綁定到哪些控件上;(把結(jié)果集中key為name對(duì)應(yīng)的對(duì)象綁定到視圖中id為name的控件上)
  listview.setAdapter(adapter);//內(nèi)部處理流程如下
  // {int total = adapter.getCount();// 獲取得到的數(shù)據(jù)總數(shù)
  // int perpage = 7;//獲取每一頁的顯示條目數(shù),
  // for (int i = 0; i < perpage; i++) {
  // View view = adapter.getView(i, convertView, parent);//第二次執(zhí)行時(shí)會(huì)將前一次執(zhí)行的view傳給convertView;
  // 顯示條目
  // }}
 }

 private void show2() {//此方法需要一個(gè)結(jié)果集中的Cursor,要求Cursor中需要有一個(gè)名稱為_id的字段;所以添加一個(gè)獲取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();//因?yàn)橐蠼Y(jié)果集中要有一個(gè)名為_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見下一章節(jié)自定義適配器
  PersonAdapter adapter = new PersonAdapter(getApplicationContext(),persons, R.layout.item);
  listview.setAdapter(adapter);
 }


當(dāng)每點(diǎn)擊一項(xiàng)需要獲取此項(xiàng)的相關(guān)信息時(shí)可以添加此方法:
listview.setOnItemClickListener(new ItemClickListener());

 private final class ItemClickListener implements OnItemClickListener {
  @Override//此方法中第一個(gè)參數(shù)為顯示數(shù)據(jù)(item項(xiàng))的控件,在此例子中即為ListView;第三個(gè)參數(shù)為點(diǎn)擊項(xiàng)的位置,
  public void onItemClick(AdapterView<?> parent, View arg1, int position,long arg3) {
   ListView lview = (ListView) parent;
   // show3()方法對(duì)應(yīng)的處理方法
   // Person person = (Person) lview.getItemAtPosition(position);
   // Toast.makeText(getApplicationContext(),person.getId().toString(),1).show();

              // show2()方法對(duì)應(yīng)的處理方法
              // show2方法中,adapter返回的是Cursor,
   // Cursor cursor = (Cursor) lview.getItemAtPosition(position);
   // int personid = cursor.getInt(cursor.getColumnIndex("_id"));
   // Toast.makeText(getApplicationContext(), personid + "", 1).show();

              // show1()方法對(duì)應(yīng)的處理方法
   // show1方法中,adapter返回的是Map,再對(duì)Map進(jìn)行操作取出相應(yīng)的id值
   HashMap<String, Object> item = (HashMap<String, Object>) lview.getItemAtPosition(position);
   int personid = (Integer) item.get("id");
   Toast.makeText(getApplicationContext(), personid + "", 1).show();
  }
 }

相關(guān)文章

最新評(píng)論