Android ListView數(shù)據(jù)綁定顯示的三種解決方法
首先,創(chuàng)建一個(gè)用于顯示一個(gè)item的layout,名為item.xml
<?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ù)的綁定,總共分為三種方法:
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;//獲取每一頁(yè)的顯示條目數(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語(yǔ)句如下!
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))的控件,在此例子中即為L(zhǎng)istView;第三個(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();
}
}
- Android DataBinding單向數(shù)據(jù)綁定深入探究
- 淺析Android企業(yè)級(jí)開發(fā)數(shù)據(jù)綁定技術(shù)
- Android Studio綁定下拉框數(shù)據(jù)詳解
- 詳解Android的MVVM框架 - 數(shù)據(jù)綁定
- Android Data Binding數(shù)據(jù)綁定詳解
- Android RecyclerView 數(shù)據(jù)綁定實(shí)例代碼
- Android中 自定義數(shù)據(jù)綁定適配器BaseAdapter的方法
- Android數(shù)據(jù)雙向綁定原理實(shí)現(xiàn)和應(yīng)用場(chǎng)景
相關(guān)文章
Android Recyclerview實(shí)現(xiàn)水平分頁(yè)GridView效果示例
本篇文章主要介紹了Android Recyclerview實(shí)現(xiàn)水平分頁(yè)GridView效果示例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08
Mac 下 Android Studio 不打印日志的解決辦法
這篇文章主要介紹了Mac 下 Android Studio 不打印日志的解決辦法的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-10-10
Android studio 快速刪除無(wú)用資源的方法
最近苦于android 項(xiàng)目越來越大,項(xiàng)目需求不停變動(dòng)。項(xiàng)目中的資源文件越來越雜,越來越亂。android studio怎么刪除無(wú)用資源呢?下文圖文并茂給大家介紹的非常詳細(xì),需要的朋友參考下吧2017-11-11
Android自定義可編輯、刪除的側(cè)滑LisitView
這篇文章主要為大家詳細(xì)介紹了Android自定義可編輯、刪除的側(cè)滑LisitView,感興趣的小伙伴們可以參考一下2016-07-07
Android添加指紋解鎖功能的實(shí)現(xiàn)代碼
當(dāng)開發(fā)的APP需要加密驗(yàn)證時(shí)可以考慮添加系統(tǒng)指紋解鎖功能。這篇文章主要介紹了Android添加指紋解鎖功能的實(shí)現(xiàn)代碼,需要的朋友可以參考下2018-07-07
kotlin中的模塊化結(jié)構(gòu)組件及工作原理
本文介紹了Kotlin中模塊化結(jié)構(gòu)組件,包括ViewModel、LiveData、Room和Navigation的工作原理和基礎(chǔ)使用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2025-03-03

