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;//獲取每一頁的顯示條目數(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();
}
}
- 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)用場景
相關(guān)文章
Android圓形頭像拍照后“無法加載此圖片”的問題解決方法(適配Android7.0)
這篇文章主要介紹了Android圓形頭像拍照后“無法加載此圖片”的問題解決方法(適配Android7.0) ,需要的朋友可以參考下2017-10-10Android實(shí)現(xiàn)狀態(tài)欄和虛擬按鍵背景顏色的變化實(shí)例代碼詳解
今天介紹一下,我在項(xiàng)目開發(fā)過程中,實(shí)現(xiàn)狀態(tài)欄和虛擬按鍵背景顏色變化的方法,實(shí)現(xiàn)方式是,通過隱藏系統(tǒng)的狀態(tài)欄和虛擬按鍵的背景,實(shí)現(xiàn)圖片和背景顯示到狀態(tài)欄和虛擬按鍵下方,需要的朋友可以參考下2019-05-05通過案例分析Android WindowManager解析與騙取QQ密碼的過程
Windows Manager是一款窗口管理終端,可以遠(yuǎn)程連接到Linux的X桌面進(jìn)行管理,與服務(wù)器端產(chǎn)生一個(gè)session相互通信,通過本文給大家分享Android WindowManager解析與騙取QQ密碼的過程,需要的朋友參考下2016-01-01Jetpack?Compose?實(shí)現(xiàn)一個(gè)圖片選擇框架功能
這篇文章主要介紹了Jetpack?Compose?實(shí)現(xiàn)一個(gè)圖片選擇框架,本文通過實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06Android單片機(jī)與藍(lán)牙模塊通信實(shí)例代碼
這篇文章主要介紹了Android單片機(jī)與藍(lán)牙模塊通信實(shí)例代碼,非常實(shí)用,特此分享給大家,需要的朋友可以參考下2016-05-05android中Glide實(shí)現(xiàn)加載圖片保存至本地并加載回調(diào)監(jiān)聽
本篇文章主要介紹了android中Glide實(shí)現(xiàn)加載圖片保存至本地并加載回調(diào)監(jiān)聽,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09