android 使用SimpleCursorAdapter将SQLite数据显示到ListView

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文件中编辑一个布局界面,下面是效果图:

android 使用SimpleCursorAdapter将SQLite数据显示到ListView_第1张图片
android 使用SimpleCursorAdapter将SQLite数据显示到ListView_第2张图片
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;
    }

}

文件目录:
android 使用SimpleCursorAdapter将SQLite数据显示到ListView_第3张图片

———————————————-分割线———————————————–
源代码:

布局文件:

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

你可能感兴趣的:(android-学习)