国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

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

2020-04-11 12:28:14
字體:
供稿:網(wǎng)友

首先,創(chuàng)建一個用于顯示一個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中,添加一個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集合中的每一個對象綁定到些item中,即將data中的每一項(xiàng)綁定到一個視圖item上;
          // 后兩個參數(shù)表示把結(jié)果集中哪些key的值綁定到哪些控件上;(把結(jié)果集中key為name對應(yīng)的對象綁定到視圖中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í)行時會將前一次執(zhí)行的view傳給convertView;
  // 顯示條目
  // }}
 }

 private void show2() {//此方法需要一個結(jié)果集中的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();//因?yàn)橐蠼Y(jié)果集中要有一個名為_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)信息時可以添加此方法:
listview.setOnItemClickListener(new ItemClickListener());

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

              // show2()方法對應(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()方法對應(yīng)的處理方法
   // show1方法中,adapter返回的是Map,再對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();
  }
 }

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宣威市| 中山市| 英山县| 陕西省| 阿克陶县| 水富县| 普兰店市| 南宁市| 青神县| 察哈| 象山县| 六枝特区| 浏阳市| 东平县| 余江县| 临西县| 石柱| 丹江口市| 淮北市| 库伦旗| 壶关县| 信阳市| 伊金霍洛旗| 体育| 湘潭市| 潮州市| 龙口市| 宣城市| 斗六市| 河北区| 浑源县| 黔南| 台前县| 裕民县| 进贤县| 新乐市| 黑山县| 宝丰县| 大竹县| 蕉岭县| 小金县|