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);
}
}