歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android采用ListView實現數據列表顯示

Android采用ListView實現數據列表顯示

日期:2017/3/1 11:17:32   编辑:Linux編程

要將數據庫中的數據列表顯示在屏幕上,我們要使用ListView這個控件,當用戶從數據庫中取出數據時,要將數據綁定到顯示控件上,如何綁定呢,我們需要創建適配器進行綁定,創建適配器有兩種方式:

第一種是用SimpleAdapter創建(要求綁定的數據是List<HashMap<String, Object>>數據類型)

第二種是用SimpleCursorAdapter創建(要求綁定的數據是Cursor數據類型)

顯示效果如圖所示:

界面布局:

item.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!--item -->
  3. <LinearLayout
  4. xmlns:Android="http://schemas.android.com/apk/res/android"
  5. android:orientation="horizontal"
  6. android:layout_width="fill_parent"
  7. android:layout_height="fill_parent">
  8. <!-- 名稱 -->
  9. <TextView
  10. android:layout_width="130dp"
  11. android:layout_height="wrap_content"
  12. android:id="@+id/name"
  13. />
  14. <!-- 電話 -->
  15. <TextView
  16. android:layout_width="150dp"
  17. android:layout_height="wrap_content"
  18. android:id="@+id/phone"
  19. />
  20. <!-- 存款 -->
  21. <TextView
  22. android:layout_width="fill_parent"
  23. android:layout_height="wrap_content"
  24. android:id="@+id/amount"
  25. />
  26. </LinearLayout>

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <!-- 標題 -->
  8. <LinearLayout
  9. android:orientation="horizontal"
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content">
  12. <TextView
  13. android:layout_width="130dp"
  14. android:layout_height="wrap_content"
  15. android:text="姓名"
  16. />
  17. <TextView
  18. android:layout_width="150dp"
  19. android:layout_height="wrap_content"
  20. android:text="電話"
  21. />
  22. <TextView
  23. android:layout_width="fill_parent"
  24. android:layout_height="wrap_content"
  25. android:text="存款"
  26. />
  27. </LinearLayout>
  28. <!-- ListView控件 -->
  29. <ListView
  30. android:layout_width="fill_parent"
  31. android:layout_height="fill_parent"
  32. android:id="@+id/listView"
  33. />
  34. </LinearLayout>

使用SimpleAdapter進行數據綁定

  1. public class MainActivity extends Activity {
  2. private PersonService service;
  3. @Override
  4. public void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.main);
  7. service = new PersonService(this);
  8. ListView listView = (ListView) this.findViewById(R.id.listView);
  9. //獲取到集合數據
  10. List<Person> persons = service.getScrollData(0, 10);
  11. List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();
  12. for(Person person : persons){
  13. HashMap<String, Object> item = new HashMap<String, Object>();
  14. item.put("id", person.getId());
  15. item.put("name", person.getName());
  16. item.put("phone", person.getPhone());
  17. item.put("amount", person.getAmount());
  18. data.add(item);
  19. }
  20. //創建SimpleAdapter適配器將數據綁定到item顯示控件上
  21. SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,
  22. new String[]{"name", "phone", "amount"}, new int[]{R.id.name, R.id.phone, R.id.amount});
  23. //實現列表的顯示
  24. listView.setAdapter(adapter);
  25. //條目點擊事件
  26. listView.setOnItemClickListener(new ItemClickListener());
  27. }
  28. //獲取點擊事件
  29. private final class ItemClickListener implements OnItemClickListener{
  30. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  31. ListView listView = (ListView) parent;
  32. HashMap<String, Object> data = (HashMap<String, Object>) listView.getItemAtPosition(position);
  33. String personid = data.get("id").toString();
  34. Toast.makeText(getApplicationContext(), personid, 1).show();
  35. }
  36. }
  37. }

使用SimpleCursorAdapter進行數據綁定

  1. public class MainActivity extends Activity {
  2. private PersonService service;
  3. @Override
  4. public void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.main);
  7. service = new PersonService(this);
  8. ListView listView = (ListView) this.findViewById(R.id.listView);
  9. //獲取游標
  10. Cursor cursor = service.getCursorScrollData(0, 10);
  11. //創建SimpleCursorAdapter適配器將數據綁定到item顯示控件上
  12. SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor,
  13. new String[]{"name", "phone", "amount"}, new int[]{R.id.name, R.id.phone, R.id.amount});
  14. listView.setAdapter(adapter);
  15. //條目點擊事件
  16. listView.setOnItemClickListener(new ItemClickListener());
  17. }
  18. private final class ItemClickListener implements OnItemClickListener{
  19. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  20. ListView listView = (ListView) parent;
  21. Cursor cursor = (Cursor) listView.getItemAtPosition(position);
  22. String personid = String.valueOf(cursor.getInt(cursor.getColumnIndex("_id")));
  23. Toast.makeText(getApplicationContext(), personid, 1).show();
  24. }
  25. }
  26. }

注意:使用第二種方式在獲取數據集合時必須指定主鍵"_id"

Copyright © Linux教程網 All Rights Reserved