Android編程使用ListView實現(xiàn)數(shù)據(jù)列表顯示的方法
本文實例講述了Android編程使用ListView實現(xiàn)數(shù)據(jù)列表顯示的方法。分享給大家供大家參考,具體如下:
要將數(shù)據(jù)庫中的數(shù)據(jù)列表顯示在屏幕上,我們要使用ListView這個控件,當用戶從數(shù)據(jù)庫中取出數(shù)據(jù)時,要將數(shù)據(jù)綁定到顯示控件上,如何綁定呢,我們需要創(chuàng)建適配器進行綁定,創(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" > <!-- 標題 --> <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進行數(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}); //實現(xiàn)列表的顯示 listView.setAdapter(adapter); //條目點擊事件 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; HashMap<String, Object> data = (HashMap<String, Object>) listView.getItemAtPosition(position); String personid = data.get("id").toString(); Toast.makeText(getApplicationContext(), personid, 1).show(); } } }
使用SimpleCursorAdapter進行數(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); //獲取游標 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); //條目點擊事件 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ù)集合時必須指定主鍵"_id"
希望本文所述對大家Android程序設計有所幫助。
相關文章
Android照片墻應用實現(xiàn) 再多的圖片也不怕崩潰
這篇文章主要為大家詳細介紹了Android照片墻應用實現(xiàn),再多的圖片也不怕崩潰,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10解決Android-RecyclerView列表倒計時錯亂問題
這篇文章主要介紹了解決Android-RecyclerView列表倒計時錯亂問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08Android中Activity和Fragment傳遞數(shù)據(jù)的兩種方式
本篇文章主要介紹了Android中Activity和Fragment傳遞數(shù)據(jù)的兩種方式,非常具有實用價值,需要的朋友可以參考下2017-09-09Fedora14下android開發(fā): eclipse與ibus確有沖突的問題分析
本篇文章是對Fedora14下android開發(fā),eclipse與ibus確有沖突的問題進行了分析介紹,需要的朋友參考下2013-05-05Android實現(xiàn)滑動刪除操作(PopupWindow)
這篇文章主要介紹了Android ListView結合PopupWindow實現(xiàn)滑動刪除的相關資料,需要的朋友可以參考下2016-07-07Android Studio finish()方法的使用與解決app點擊“返回”(直接退出)
這篇文章主要介紹了Android Studio finish()方法的使用與解決app點擊“返回”(直接退出),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04