ListView实现数据列表显示
要将数据库中的数据列表显示在屏幕上,我们要使用ListView这个控件,当用户从数据库中取出数据时,要将数据绑定到显示控件上,如何绑定呢,我们需要创建适配器进行绑定,创建适配器有两种方式:
第一种是用SimpleAdapter创建(要求绑定的数据是List<HashMap<String, Object>>数据类型)
第二种是用SimpleCursorAdapter创建(要求绑定的数据是Cursor数据类型)
显示效果如图所示:

界面布局:
item.xml
- <?xml version="1.0" encoding="utf-8"?>
- <!--item -->
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <!-- 名称 -->
- <TextView
- android:layout_width="130dp"
- android:layout_height="wrap_content"
- android:id="@+id/name"
- />
- <!-- 电话 -->
- <TextView
- android:layout_width="150dp"
- android:layout_height="wrap_content"
- android:id="@+id/phone"
- />
- <!-- 存款 -->
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/amount"
- />
- </LinearLayout>
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <!-- 标题 -->
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <TextView
- android:layout_width="130dp"
- android:layout_height="wrap_content"
- android:text="姓名"
- />
- <TextView
- android:layout_width="150dp"
- android:layout_height="wrap_content"
- android:text="电话"
- />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="存款"
- />
- </LinearLayout>
- <!-- ListView控件 -->
- <ListView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:id="@+id/listView"
- />
- </LinearLayout>
使用SimpleAdapter进行数据绑定
- public class MainActivity extends Activity {
- private PersonService service;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- service = new PersonService(this);
- ListView listView = (ListView) this.findViewById(R.id.listView);
- //获取到集合数据
- List<Person> persons = service.getScrollData(0, 10);
- List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();
- for(Person person : persons){
- HashMap<String, Object> item = new HashMap<String, Object>();
- item.put("id", person.getId());
- item.put("name", person.getName());
- item.put("phone", person.getPhone());
- item.put("amount", person.getAmount());
- data.add(item);
- }
- //创建SimpleAdapter适配器将数据绑定到item显示控件上
- SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,
- new String[]{"name", "phone", "amount"}, new int[]{R.id.name, R.id.phone, R.id.amount});
- //实现列表的显示
- listView.setAdapter(adapter);
- //条目点击事件
- listView.setOnItemClickListener(new ItemClickListener());
- }
- //获取点击事件
- private final class ItemClickListener implements OnItemClickListener{
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- ListView listView = (ListView) parent;
- HashMap<String, Object> data = (HashMap<String, Object>) listView.getItemAtPosition(position);
- String personid = data.get("id").toString();
- Toast.makeText(getApplicationContext(), personid, 1).show();
- }
- }
- }
使用SimpleCursorAdapter进行数据绑定
- public class MainActivity extends Activity {
- private PersonService service;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- service = new PersonService(this);
- ListView listView = (ListView) this.findViewById(R.id.listView);
- //获取游标
- Cursor cursor = service.getCursorScrollData(0, 10);
- //创建SimpleCursorAdapter适配器将数据绑定到item显示控件上
- SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor,
- new String[]{"name", "phone", "amount"}, new int[]{R.id.name, R.id.phone, R.id.amount});
- listView.setAdapter(adapter);
- //条目点击事件
- listView.setOnItemClickListener(new ItemClickListener());
- }
- private final class ItemClickListener implements OnItemClickListener{
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- ListView listView = (ListView) parent;
- Cursor cursor = (Cursor) listView.getItemAtPosition(position);
- String personid = String.valueOf(cursor.getInt(cursor.getColumnIndex("_id")));
- Toast.makeText(getApplicationContext(), personid, 1).show();
- }
- }
- }
注意:使用第二种方式在获取数据集合时必须指定主键"_id"
ListView实现数据列表显示的更多相关文章
- 黎活明8天快速掌握android视频教程--19_采用ListView实现数据列表显示
1.首先整个程序也是采用mvc的框架 DbOpenHelper 类 package dB; import android.content.Context; import android.databas ...
- C#使用ListView更新数据出现闪烁解决办法
C#使用ListView更新数据出现闪烁解决办法 在使用vs自动控件ListView控件时候,更新里面的部分代码时候出现闪烁的情况 如图: 解决以后: 解决办法使用双缓冲:添加新类继承ListView ...
- 使用自定的Adapter绑定ListView/GridView数据
使用自定的Adapter绑定ListView/GridView数据(地址) 对于ListView/Gridview的数据绑定, google提供了一些Adapter的模板, 自己也可以自定义一些个性化 ...
- Android 根据EditText搜索框ListView动态显示数据
根据EditText搜索框ListView动态显示数据是根据需求来的,觉得这之中涉及的东西可能比较的有意思,所以动手来写一写,希望对大家有点帮助. 首先,我们来分析下整个过程: 1.建立一个layou ...
- Android之listview添加数据篇
一.ListView: 1. ListView通常有两个职责: 1.向布局填充数据 2.处理选择点击等操作 2.ListView的创建需要3个元素: 1. ListView中的每一列的View. 2. ...
- Android开发之对ListView的数据进行排序
这里涉及到对ListView的数据进行排序,以及ListView的数据如何清空处理.排序的方法相同,但是里面的数据集合有些区别:一种是利用pojo类取得数据:另一种是利用map来取得数据. 第一种:利 ...
- Android ListView绑定数据
ListView绑定数据的三层: ListView绑定数据源从逻辑上可以分为三层:UI层,逻辑层,数据层. UI层:UI层即是ListView控件. 数据层:要展示到ListView控件上面的数据. ...
- Android 依据EditText搜索框ListView动态显示数据
依据EditText搜索框ListView动态显示数据是依据需求来的,认为这之中涉及的东西可能比較的有意思,所以动手来写一写.希望对大家有点帮助. 首先.我们来分析下整个过程: 1.建立一个layou ...
- WPF - 多列ListView添加数据的多种方式
多列ListView: <ListView x:Name="listView"> <ListView.View> <GridView> < ...
随机推荐
- 撩课-Java每天5道面试题第11天
86.如何获得高效的数据库逻辑结构? 从关系数据库的表中 删除冗余信息的过程 称为数据规范化, 是得到高效的关系型数据库表的逻辑结构 最好和最容易的方法. 规范化数据时应执行以下操作: 1.将数据库的 ...
- UVA 350 Pseudo-Random Numbers
Pseudo-Random Numbers Computers normally cannot generate really random numbers, but frequently are ...
- 快速定位问题 Request无法获取参数
比如说最近开发甲修改了iframe标签的src,开发乙在设置src的时候传入了2个参数,通过iframe标签链接到这个页面时,开发乙调试时发现没有拿到任何参数值.然后开发乙百度了一下,发现iframe ...
- [置顶] 解决EXTJS文本框长度验证在ORACLE数据库下不正确的问题
由于ORACLE数据库里面一个汉字和符号占2 个字节,数字和英文占1个字节,所以用EXTJS的文本框MaxLenght去限制输入的长度是不正确的,因为EXTJS只限制了输入的字数量,而不是字节数量. ...
- 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 ...
- PostgreSQL 资料库
https://yq.aliyun.com/articles/59251 https://github.com/digoal/blog/blob/master/201609/20160929_02.m ...
- oracle 锁系列
http://www.cnblogs.com/lhrbest/p/6091277.html
- 常见 core dump 原因分析signal 11 - SIGSEGV
signal 6 - SIGABRT free 多次 char *p = malloc(100); free(p); free(p); fclose 多次 // fclose 内部调用 free FI ...
- MVC文件上传08-使用客户端jQuery-File-Upload插件和服务端Backload组件让每个用户有专属文件夹
当需要为每个用户建立一个专属上传文件夹的时候,可以在提交文件的视图中添加一个隐藏域,并设置name="objectContext". 相关兄弟篇: MVC文件上传01-使用jque ...
- envi几何校正
转载自原文 介绍地理参考数据的知识以及ENVI 中图像对图像.图像对地图两种校正方法 1.打开基图像XX.img和待纠正的图像YY.img(不带地理信息,可以双击其主图像窗口可以在Cursor Loc ...