要将数据库中的数据列表显示在屏幕上,我们要使用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. C++之lambda理解

    简介 在C++ Primer中,是这样定义的-一个lambda表达式表示一个可调用的代码单元,可以将其理解为一个未命名的内联函数:与任何函数类似,一个lambda具有一个返回类型,一个参数列表和一个函 ...

  2. Java NIo 笔记001

    1. Channel Channel接口只提供了两个方法: package java.nio.channels; public interface Channel { public boolean i ...

  3. hrbust 2176 Mac的投票 二分/水题

    Mac的投票 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 52(12 users) Total Accepted: 12(10 us ...

  4. angular 自定义指令参数详解【转】【个人收藏用】

    restrict:指令在dom中的声明形式 E(元素)A(属性)C(类名)M(注释) priority优先级:一个元素上存在两个指令,来决定那个指令被优先执行 terminal:true或false, ...

  5. Android - Mount a Samba share

    Mount Manager, Cifs manager :Manage your CIFS/NFS network shares was working, but the command from t ...

  6. systemtap 2.8 安装说明书

    systemtap: a linux trace/probe tool Visit the project web site at <http://sourceware.org/systemta ...

  7. STM32学习笔记3-IO配置输入输出

    STM32的IO配置时没什么特殊的,有个注意点就是有用IO前须要先打开其时钟线,下面是验证过oK的程序: RCC->APB2ENR|=GpioBApb2enrEn; //使能PORTB时钟 GP ...

  8. error MSB8031: Building an MFC project for a non-Unicode character set is deprecated

    vs2013编译VC++源码,错误: error MSB8031: Building an MFC project for a non-Unicode character set is depreca ...

  9. 【c语言】模拟实现库函数的atof函数

    // 模拟实现库函数的atof函数 #include <stdio.h> #include <string.h> #include <assert.h> #incl ...

  10. RobotFramework自动化3-搜索案例

    前言 RF系列主要以案例为主,关键字不会的可以多按按F5,里面都有很详细的介绍,要是纯翻译的话,就没太大意义了,因为小编本来英语就很差哦! 前面selenium第八篇介绍过定位一组搜索结果,是拿百度搜 ...