ListView实现数据列表显示

时间:2023-03-08 21:06:49

要将数据库中的数据列表显示在屏幕上,我们要使用ListView这个控件,当用户从数据库中取出数据时,要将数据绑定到显示控件上,如何绑定呢,我们需要创建适配器进行绑定,创建适配器有两种方式:

第一种是用SimpleAdapter创建(要求绑定的数据是List<HashMap<String, Object>>数据类型)

第二种是用SimpleCursorAdapter创建(要求绑定的数据是Cursor数据类型)

显示效果如图所示:

ListView实现数据列表显示

界面布局:

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"