android 使用SimpleCursorAdapter将SQLite数据显示到ListView
来源:互联网 发布:c4d软件介绍 编辑:程序博客网 时间:2024/05/16 08:33
android 使用SimpleCursorAdapter将SQLite数据显示到ListView
我们知道,使用ListView的时候需要一个数据源,可以是本地数据,可以是网络数据。本篇博文使用SQLite为ListView提供数据源。
一、首先我们需要创建一个数据库表格。 a、建立一个类 DBHelper 继承 SQLiteOpenHelper .
public class DBHelper extends SQLiteOpenHelper { public DBHelper(Context context) { super(context, "test2.db", null, 1); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub String sql = "create table class(_id integer primary key autoincrement,name varchar(64),number varchar(64))"; //要使用游标适配器,SQLite表格必须包含一栏“_id” db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub }}
关于SQLite的使用请访问我的另外一篇博文http://blog.csdn.net/q296264785/article/details/53155739
用单元测试的方法给数据库添加一些数据,方便看到效果。
public void add() { DBHelper dbHelper = new DBHelper(getContext()); SQLiteDatabase database = dbHelper.getReadableDatabase(); ContentValues values = new ContentValues(); values.put("name", "张利利"); values.put("number", "12345"); database.insert("class", null, values); values.put("name", "李提莫"); values.put("number", "43455"); database.insert("class", null, values); values.put("name", "王石说"); values.put("number", "42345"); database.insert("class", null, values); values.put("name", "赵六壳"); values.put("number", "14215"); database.insert("class", null, values); values.put("name", "郭撒大"); values.put("number", "45234"); database.insert("class", null, values); values.put("name", "刘䮻明"); values.put("number", "23445"); database.insert("class", null, values); }
c、在XML文件中编辑一个布局界面,下面是效果图:
XML布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#cccccc" android:paddingBottom="10sp" > <TextView android:id="@+id/textView1" android:layout_width="0sp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:paddingTop="10dp" android:text="编号" android:textColor="#2828ff" android:textSize="20sp" /> <TextView android:id="@+id/textView2" android:layout_width="0sp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:paddingTop="10dp" android:text="姓名" android:textColor="#2828ff" android:textSize="20sp" /> <TextView android:id="@+id/textView3" android:layout_width="0sp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:paddingTop="10dp" android:text="工号" android:textColor="#2828ff" android:textSize="20sp" /> </LinearLayout> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView></LinearLayout>
d、在Activity中创建一个SimpleCursorAdapter,并且设置给ListView。
import android.app.Activity;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.view.Menu;import android.widget.ListView;import android.widget.SimpleCursorAdapter;public class MainActivity extends Activity { private DBHelper dbHelper; private SQLiteDatabase database; private SimpleCursorAdapter adapter;// 简单的游标适配器 private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listView1); dbHelper = new DBHelper(this); database = dbHelper.getReadableDatabase(); Cursor c = database.query("class", null, null, null, null, null, null); String[] from = { "_id", "name", "number" }; int[] to = { R.id.textView1, R.id.textView2, R.id.textView3 }; adapter = new SimpleCursorAdapter(this, R.layout.activity_main, c, from, to);//改方法在搞版本已经过时,因为如果读取数据库时间过长是系统会报错,新的方法将数据库读取操作放在另外的线程中。 listView.setAdapter(adapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }}
文件目录:
———————————————-分割线———————————————–
源代码:
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#cccccc" android:paddingBottom="10sp" > <TextView android:id="@+id/textView1" android:layout_width="0sp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:paddingTop="10dp" android:text="编号" android:textColor="#2828ff" android:textSize="20sp" /> <TextView android:id="@+id/textView2" android:layout_width="0sp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:paddingTop="10dp" android:text="姓名" android:textColor="#2828ff" android:textSize="20sp" /> <TextView android:id="@+id/textView3" android:layout_width="0sp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:paddingTop="10dp" android:text="工号" android:textColor="#2828ff" android:textSize="20sp" /> </LinearLayout> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView></LinearLayout>
DBHelper 类:
import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteDatabase.CursorFactory;import android.database.sqlite.SQLiteOpenHelper;public class DBHelper extends SQLiteOpenHelper { public DBHelper(Context context) { super(context, "test2.db", null, 1); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub String sql = "create table class(_id integer primary key autoincrement,name varchar(64),number varchar(64))"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub }}
MainActivity:
import android.app.Activity;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.view.Menu;import android.widget.ListView;import android.widget.SimpleCursorAdapter;public class MainActivity extends Activity { private DBHelper dbHelper; private SQLiteDatabase database; private SimpleCursorAdapter adapter;// 简单的游标适配器 private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listView1); dbHelper = new DBHelper(this); database = dbHelper.getReadableDatabase(); Cursor c = database.query("class", null, null, null, null, null, null); String[] from = { "_id", "name", "number" }; int[] to = { R.id.textView1, R.id.textView2, R.id.textView3 }; adapter = new SimpleCursorAdapter(this, R.layout.activity_main, c, from, to);//该方法在高的版本中已经过时,因为在如果读取数据库时间过长系统会报错,新的版本中将数据库读取操作放在另外的线程中。 listView.setAdapter(adapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }}
测试类test
import android.content.ContentValues;import android.database.sqlite.SQLiteDatabase;import android.test.AndroidTestCase;public class test extends AndroidTestCase { public void create() { DBHelper dbHelper = new DBHelper(getContext()); SQLiteDatabase database = dbHelper.getReadableDatabase(); } public void add() { DBHelper dbHelper = new DBHelper(getContext()); SQLiteDatabase database = dbHelper.getReadableDatabase(); ContentValues values = new ContentValues(); values.put("name", "张利利"); values.put("number", "12345"); database.insert("class", null, values); values.put("name", "李提莫"); values.put("number", "43455"); database.insert("class", null, values); values.put("name", "王石说"); values.put("number", "42345"); database.insert("class", null, values); values.put("name", "赵六壳"); values.put("number", "14215"); database.insert("class", null, values); values.put("name", "郭撒大"); values.put("number", "45234"); database.insert("class", null, values); values.put("name", "刘䮻明"); values.put("number", "23445"); database.insert("class", null, values); }}
1 0
- android 使用SimpleCursorAdapter将SQLite数据显示到ListView
- SQLite数据查询结果显示在ListView(SimpleAdapter与SimpleCursorAdapter)
- Android使用ListView将数据显示到手机屏幕上
- 使用SimpleCursorAdapter绑定显示listview
- ListView显示数据库的数据-SimpleCursorAdapter的使用
- Android-使用ListView把SQLite中的数据显示至屏幕
- Android-使用ListView把SQLite中的数据显示至屏幕
- 使用SimpleCursorAdapter的ListView如何更新显示
- <学习笔记>Android使用Loader加载sqlite数据到ListView
- android SQlite结合SimpleCursorAdapter的简单使用
- android listview 用simplecursoradapter适配器数据刷新
- android——ListView(使用simpleCursorAdapter)
- Android SQLite数据显示于ListView
- Android + sqlite simplecursorAdapter Demo
- 采用ListView实现数据列表显示——SimpleCursorAdapter
- android ListView,SimpleCursorAdapter
- 安卓:将数据库中的数据查询出来用adapter(SimpleAdapter,SimpleCursorAdapter,自定义适配器CursorAdapter)绑定到listview上
- Android listView显示SQlite
- http状态码查询 http_code状态码大全《详细》
- free命令详解
- 数据结构---队列
- C语言结构体
- AES加密-解密
- android 使用SimpleCursorAdapter将SQLite数据显示到ListView
- 在定义了外部layout的事件后,处理内部所包含的部件的事件时没触发。
- 全面FreeMarker教程
- 虚方法&非虚方法
- mac系统PHP开发环境搭建
- Unreal Engine 4切换默认Camera实现
- 安卓压力测试
- android系统加速以及修改lanucher
- oracle中decode函数的使用