要将数据库中的数据列表显示在屏幕上,我们要使用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"

ListView实现数据列表显示的更多相关文章

  1. 黎活明8天快速掌握android视频教程--19_采用ListView实现数据列表显示

    1.首先整个程序也是采用mvc的框架 DbOpenHelper 类 package dB; import android.content.Context; import android.databas ...

  2. C#使用ListView更新数据出现闪烁解决办法

    C#使用ListView更新数据出现闪烁解决办法 在使用vs自动控件ListView控件时候,更新里面的部分代码时候出现闪烁的情况 如图: 解决以后: 解决办法使用双缓冲:添加新类继承ListView ...

  3. 使用自定的Adapter绑定ListView/GridView数据

    使用自定的Adapter绑定ListView/GridView数据(地址) 对于ListView/Gridview的数据绑定, google提供了一些Adapter的模板, 自己也可以自定义一些个性化 ...

  4. Android 根据EditText搜索框ListView动态显示数据

    根据EditText搜索框ListView动态显示数据是根据需求来的,觉得这之中涉及的东西可能比较的有意思,所以动手来写一写,希望对大家有点帮助. 首先,我们来分析下整个过程: 1.建立一个layou ...

  5. Android之listview添加数据篇

    一.ListView: 1. ListView通常有两个职责: 1.向布局填充数据 2.处理选择点击等操作 2.ListView的创建需要3个元素: 1. ListView中的每一列的View. 2. ...

  6. Android开发之对ListView的数据进行排序

    这里涉及到对ListView的数据进行排序,以及ListView的数据如何清空处理.排序的方法相同,但是里面的数据集合有些区别:一种是利用pojo类取得数据:另一种是利用map来取得数据. 第一种:利 ...

  7. Android ListView绑定数据

    ListView绑定数据的三层: ListView绑定数据源从逻辑上可以分为三层:UI层,逻辑层,数据层. UI层:UI层即是ListView控件. 数据层:要展示到ListView控件上面的数据. ...

  8. Android 依据EditText搜索框ListView动态显示数据

    依据EditText搜索框ListView动态显示数据是依据需求来的,认为这之中涉及的东西可能比較的有意思,所以动手来写一写.希望对大家有点帮助. 首先.我们来分析下整个过程: 1.建立一个layou ...

  9. WPF - 多列ListView添加数据的多种方式

    多列ListView: <ListView x:Name="listView"> <ListView.View> <GridView> < ...

随机推荐

  1. hdu 4417 区间内比h小的数 线段树

    题意求区间内比h小的数的个数 将所有的询问离线读入之后,按H从小到大排序.然后对于所有的结点也按从小到大排序,然后根据查询的H,将比H小的点加入到线段树,然后就是一个区间和. 2015-07-27:专 ...

  2. Codechef December Challenge 2014 Chef and Apple Trees 水题

    Chef and Apple Trees Chef loves to prepare delicious dishes. This time, Chef has decided to prepare ...

  3. 如何使用mysql存储树形关系

    最近遇到业务的一个类似文件系统的存储需求,对于如何在mysql中存储一颗树进行了一些讨论,分享一下,看看有没有更优的解决方案. 一.现有情况 首先,先假设有这么一颗树,一共9个节点,1是root节点, ...

  4. QN-H618 遥控器复制再生仪(拷贝机)

    针对现在市场上日益更新的遥控器种类,本公司经过长时间的研究,推出新一代拷贝机,本产品有以下特点: 1. 众多车库门遥控分析信息被集成在一台机器内,只要一种遥控器,就可以复制众多品牌的车库遥控.免去积压 ...

  5. UVa409_Excuses, Excuses!(小白书字符串专题)

    解题报告 题意: 找包括单词最多的串.有多个按顺序输出 思路: 字典树爆. #include <cstdio> #include <cstring> #include < ...

  6. [MySql]默认密码的查找与修改

    摘要 在安装成功后,怎么找到mysql的默认密码,折腾很长时间,最后发现在安装的过程中,产生了一个默认的随机密码. 密码 在mysql安装目录生成的data文件下,查找xxx.err的文件如图: 用记 ...

  7. 树状结构 Tree data structure in C#

    delegate void TreeVisitor<T>(T nodeData); class NTree<T> { private T data; private Linke ...

  8. jsp页面传递参数是如何与javabean进行关联的

    总结:1.severlet容器是通过JavaBean中的属性方法名来获取属性名的,然后根据此属性名来从request中取值 2.JavaBean中属性方法的命名,set后的名称要与你从request中 ...

  9. [翻译] FMDB

    FMDB https://github.com/ccgus/fmdb This is an Objective-C wrapper around SQLite: http://sqlite.org/ ...

  10. hue耗流量优化

    ps: 使用的hue版本为 hue-3.10.0 一.[jobbrowser刷流量] 基本一分钟刷新一次,执行GET /jobbrowser/ [17/Apr/2017 14:46:26 +0800] ...