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> < ...
随机推荐
- C++之lambda理解
简介 在C++ Primer中,是这样定义的-一个lambda表达式表示一个可调用的代码单元,可以将其理解为一个未命名的内联函数:与任何函数类似,一个lambda具有一个返回类型,一个参数列表和一个函 ...
- Java NIo 笔记001
1. Channel Channel接口只提供了两个方法: package java.nio.channels; public interface Channel { public boolean i ...
- hrbust 2176 Mac的投票 二分/水题
Mac的投票 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 52(12 users) Total Accepted: 12(10 us ...
- angular 自定义指令参数详解【转】【个人收藏用】
restrict:指令在dom中的声明形式 E(元素)A(属性)C(类名)M(注释) priority优先级:一个元素上存在两个指令,来决定那个指令被优先执行 terminal:true或false, ...
- Android - Mount a Samba share
Mount Manager, Cifs manager :Manage your CIFS/NFS network shares was working, but the command from t ...
- systemtap 2.8 安装说明书
systemtap: a linux trace/probe tool Visit the project web site at <http://sourceware.org/systemta ...
- STM32学习笔记3-IO配置输入输出
STM32的IO配置时没什么特殊的,有个注意点就是有用IO前须要先打开其时钟线,下面是验证过oK的程序: RCC->APB2ENR|=GpioBApb2enrEn; //使能PORTB时钟 GP ...
- 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 ...
- 【c语言】模拟实现库函数的atof函数
// 模拟实现库函数的atof函数 #include <stdio.h> #include <string.h> #include <assert.h> #incl ...
- RobotFramework自动化3-搜索案例
前言 RF系列主要以案例为主,关键字不会的可以多按按F5,里面都有很详细的介绍,要是纯翻译的话,就没太大意义了,因为小编本来英语就很差哦! 前面selenium第八篇介绍过定位一组搜索结果,是拿百度搜 ...