要将数据库中的数据列表显示在屏幕上,我们要使用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. 撩课-Java每天5道面试题第11天

    86.如何获得高效的数据库逻辑结构? 从关系数据库的表中 删除冗余信息的过程 称为数据规范化, 是得到高效的关系型数据库表的逻辑结构 最好和最容易的方法. 规范化数据时应执行以下操作: 1.将数据库的 ...

  2. UVA 350 Pseudo-Random Numbers

     Pseudo-Random Numbers  Computers normally cannot generate really random numbers, but frequently are ...

  3. 快速定位问题 Request无法获取参数

    比如说最近开发甲修改了iframe标签的src,开发乙在设置src的时候传入了2个参数,通过iframe标签链接到这个页面时,开发乙调试时发现没有拿到任何参数值.然后开发乙百度了一下,发现iframe ...

  4. [置顶] 解决EXTJS文本框长度验证在ORACLE数据库下不正确的问题

    由于ORACLE数据库里面一个汉字和符号占2 个字节,数字和英文占1个字节,所以用EXTJS的文本框MaxLenght去限制输入的长度是不正确的,因为EXTJS只限制了输入的字数量,而不是字节数量. ...

  5. chrome --headless --disable-gpu --dump-dom http://www.python.org

    Driving Headless Chrome with Python:Python chrome --headless --disable-gpu --dump-dom http://www.pyt ...

  6. PostgreSQL 资料库

    https://yq.aliyun.com/articles/59251 https://github.com/digoal/blog/blob/master/201609/20160929_02.m ...

  7. oracle 锁系列

    http://www.cnblogs.com/lhrbest/p/6091277.html

  8. 常见 core dump 原因分析signal 11 - SIGSEGV

    signal 6 - SIGABRT free 多次 char *p = malloc(100); free(p); free(p); fclose 多次 // fclose 内部调用 free FI ...

  9. MVC文件上传08-使用客户端jQuery-File-Upload插件和服务端Backload组件让每个用户有专属文件夹

    当需要为每个用户建立一个专属上传文件夹的时候,可以在提交文件的视图中添加一个隐藏域,并设置name="objectContext". 相关兄弟篇: MVC文件上传01-使用jque ...

  10. envi几何校正

    转载自原文 介绍地理参考数据的知识以及ENVI 中图像对图像.图像对地图两种校正方法 1.打开基图像XX.img和待纠正的图像YY.img(不带地理信息,可以双击其主图像窗口可以在Cursor Loc ...