Android編程使用ListView實(shí)現(xiàn)數(shù)據(jù)列表顯示的方法
本文實(shí)例講述了Android編程使用ListView實(shí)現(xiàn)數(shù)據(jù)列表顯示的方法。分享給大家供大家參考,具體如下:
要將數(shù)據(jù)庫(kù)中的數(shù)據(jù)列表顯示在屏幕上,我們要使用ListView這個(gè)控件,當(dāng)用戶從數(shù)據(jù)庫(kù)中取出數(shù)據(jù)時(shí),要將數(shù)據(jù)綁定到顯示控件上,如何綁定呢,我們需要?jiǎng)?chuàng)建適配器進(jìn)行綁定,創(chuàng)建適配器有兩種方式:
第一種是用SimpleAdapter創(chuàng)建(要求綁定的數(shù)據(jù)是List<HashMap<String, Object>>數(shù)據(jù)類型)
第二種是用SimpleCursorAdapter創(chuàng)建(要求綁定的數(shù)據(jù)是Cursor數(shù)據(jù)類型)
顯示效果如圖所示:
界面布局:
item.xml
<?xml version="1.0" encoding="utf-8"?> <!--item --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- 名稱 --> <TextView android:layout_width="130dp" android:layout_height="wrap_content" android:id="@+id/name" /> <!-- 電話 --> <TextView android:layout_width="150dp" android:layout_height="wrap_content" android:id="@+id/phone" /> <!-- 存款 --> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/amount" /> </LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- 標(biāo)題 --> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="130dp" android:layout_height="wrap_content" android:text="姓名" /> <TextView android:layout_width="150dp" android:layout_height="wrap_content" android:text="電話" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="存款" /> </LinearLayout> <!-- ListView控件 --> <ListView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/listView" /> </LinearLayout>
使用SimpleAdapter進(jìn)行數(shù)據(jù)綁定
public class MainActivity extends Activity { private PersonService service; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); service = new PersonService(this); ListView listView = (ListView) this.findViewById(R.id.listView); //獲取到集合數(shù)據(jù) List<Person> persons = service.getScrollData(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("id", person.getId()); item.put("name", person.getName()); item.put("phone", person.getPhone()); item.put("amount", person.getAmount()); data.add(item); } //創(chuàng)建SimpleAdapter適配器將數(shù)據(jù)綁定到item顯示控件上 SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item, new String[]{"name", "phone", "amount"}, new int[]{R.id.name, R.id.phone, R.id.amount}); //實(shí)現(xiàn)列表的顯示 listView.setAdapter(adapter); //條目點(diǎn)擊事件 listView.setOnItemClickListener(new ItemClickListener()); } //獲取點(diǎn)擊事件 private final class ItemClickListener implements OnItemClickListener{ public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ListView listView = (ListView) parent; HashMap<String, Object> data = (HashMap<String, Object>) listView.getItemAtPosition(position); String personid = data.get("id").toString(); Toast.makeText(getApplicationContext(), personid, 1).show(); } } }
使用SimpleCursorAdapter進(jìn)行數(shù)據(jù)綁定
public class MainActivity extends Activity { private PersonService service; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); service = new PersonService(this); ListView listView = (ListView) this.findViewById(R.id.listView); //獲取游標(biāo) Cursor cursor = service.getCursorScrollData(0, 10); //創(chuàng)建SimpleCursorAdapter適配器將數(shù)據(jù)綁定到item顯示控件上 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); //條目點(diǎn)擊事件 listView.setOnItemClickListener(new ItemClickListener()); } private final class ItemClickListener implements OnItemClickListener{ public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ListView listView = (ListView) parent; Cursor cursor = (Cursor) listView.getItemAtPosition(position); String personid = String.valueOf(cursor.getInt(cursor.getColumnIndex("_id"))); Toast.makeText(getApplicationContext(), personid, 1).show(); } } }
注意:使用第二種方式在獲取數(shù)據(jù)集合時(shí)必須指定主鍵"_id"
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android照片墻應(yīng)用實(shí)現(xiàn) 再多的圖片也不怕崩潰
這篇文章主要為大家詳細(xì)介紹了Android照片墻應(yīng)用實(shí)現(xiàn),再多的圖片也不怕崩潰,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10Android冷啟動(dòng)優(yōu)化的3個(gè)小案例分享
為了提高App的冷啟動(dòng)耗時(shí),除了在常規(guī)的業(yè)務(wù)側(cè)進(jìn)行耗時(shí)代碼優(yōu)化之外,為了進(jìn)一步縮短啟動(dòng)耗時(shí),需要在純技術(shù)測(cè)做一些優(yōu)化探索,本期我們從類預(yù)加載、Retrofit 、ARouter方面進(jìn)行了進(jìn)一步的優(yōu)化,感興趣的同學(xué)跟著小編一起來(lái)看看吧2023-07-07解決Android-RecyclerView列表倒計(jì)時(shí)錯(cuò)亂問(wèn)題
這篇文章主要介紹了解決Android-RecyclerView列表倒計(jì)時(shí)錯(cuò)亂問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08Android中Activity和Fragment傳遞數(shù)據(jù)的兩種方式
本篇文章主要介紹了Android中Activity和Fragment傳遞數(shù)據(jù)的兩種方式,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-09-09Fedora14下android開(kāi)發(fā): eclipse與ibus確有沖突的問(wèn)題分析
本篇文章是對(duì)Fedora14下android開(kāi)發(fā),eclipse與ibus確有沖突的問(wèn)題進(jìn)行了分析介紹,需要的朋友參考下2013-05-05Android實(shí)現(xiàn)滑動(dòng)刪除操作(PopupWindow)
這篇文章主要介紹了Android ListView結(jié)合PopupWindow實(shí)現(xiàn)滑動(dòng)刪除的相關(guān)資料,需要的朋友可以參考下2016-07-07Android Studio finish()方法的使用與解決app點(diǎn)擊“返回”(直接退出)
這篇文章主要介紹了Android Studio finish()方法的使用與解決app點(diǎn)擊“返回”(直接退出),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04