本文主要介绍数据库与listview绑定的两种方法,把从数据库获取到的数据显示到listview界面上。详细请看代码:
主要参考文章地址:http://blog.youkuaiyun.com/furongkang/article/details/6819247
要将数据库中的数据列表显示在屏幕上,我们要使用ListView这个控件,当用户从数据库中取出数据时,要将数据绑定到显示控件上,如何绑定呢,我们需要创建适配器进行绑定,创建适配器有两种方式:
第一种是用SimpleAdapter创建(要求绑定的数据是List<HashMap<String, Object>>数据类型);
第二种是用SimpleCursorAdapter创建(要求绑定的数据是Cursor数据类型)。
1.布局文件1: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/count"
/>
</LinearLayout>
2.布局文件2:get_info.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="24dip"
android:layout_gravity="center"
android:text="获取服务器端数据测试 "/>
<Button
android:id="@+id/download_xml"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="从FTP服务器下载XML文件"/>
<!-- <TextView
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>-->
<!-- 标题 -->
<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="使用次数"
/>
</LinearLayout>
<!-- ListView控件 -->
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/listView"
/>
</LinearLayout>
3.(一)使用SimpleAdapter进行数据绑定
//获取到集合数据
List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();
for(AppInfo app : allNews){
HashMap<String, Object> item = new HashMap<String, Object>();
item.put("name", app.getName());
item.put("count", app.getCount());
data.add(item);
}
//创建SimpleAdapter适配器将数据绑定到item显示控件上
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,
new String[]{"name", "count"}, new int[]{R.id.name, R.id.count});
//实现列表的显示
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 appname = data.get("name").toString();
Toast.makeText(getApplicationContext(), appname+data.get("count").toString(), Toast.LENGTH_SHORT).show();
}
3.(二)使用SimpleCursorAdapter进行数据绑定
List<AppInfo> allNews = DataSupport.findAll(AppInfo.class);
Log.d(TAG, "取出所有数据:"+allNews.toString());
SQLiteDatabase db = Connector.getReadableDatabase(); //创建表
// List<AppInfo> appList = (List<AppInfo>) DataSupport.select("select AppName,sum(count) from AppInfo group by AppName").find(AppInfo.class);
String sql="select name as _id , sum(count) from AppInfo group by name";//,sum(count) as TOTAL
// String sql="select _id from AppInfo";
Cursor a=db.rawQuery(sql,null);
Log.d(TAG, "取出所有数据后:"+ a.toString());
if(a.moveToFirst())
{
//创建SimpleCursorAdapter适配器将数据绑定到item显示控件上
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, a,
new String[]{"_id", "count"}, new int[]{R.id.name, R.id.count});
Log.d(TAG, "测试2");
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);
Log.d(TAG, "测试3");
String appname = String.valueOf(cursor.getInt(cursor.getColumnIndex("_id")));
Log.d(TAG, "测试4");
Toast.makeText(getApplicationContext(), appname, Toast.LENGTH_SHORT).show();
}
}
注意:使用第二种方式在获取数据集合时必须指定主键"_id"