光标适配器和sqlite的例子

您好,我正在寻找示例代码中使用的游标适配器与sqlite?

真的很简单的例子。

这是一个非常简单但非常有效的例子。 一旦你掌握了基础知识,你就可以很容易地build立起来。

在SQLite中使用光标适配器有两个主要部分:

  1. 从数据库创build一个适当的光标

  2. 创build一个自定义光标适配器 ,该适配器从数据库获取光标数据,并将其与您希望用来表示数据的View配对。

1.从数据库创build一个适当的光标。

在您的活动中:

SQLiteOpenHelper sqLiteOpenHelper = new SQLiteOpenHelper( context, DATABASE_NAME, null, DATABASE_VERSION); SQLiteDatabase sqLiteDatabase = sqLiteOpenHelper.getReadableDatabase(); String query = "SELECT * FROM clients ORDER BY company_name ASC"; // No trailing ';' Cursor cursor = sqLiteDatabase.rawQuery(query, null); ClientCursorAdapter adapter = new ClientCursorAdapter( this, R.layout.clients_listview_row, cursor, 0 ); this.setListAdapter(adapter); 

2.创build一个自定义光标适配器。

注意:从ResourceCursorAdapter扩展假定您使用XML来创build视图。

 public class ClientCursorAdapter extends ResourceCursorAdapter { public ClientCursorAdapter(Context context, int layout, Cursor cursor, int flags) { super(context, layout, cursor, flags); } @Override public void bindView(View view, Context context, Cursor cursor) { TextView name = (TextView) view.findViewById(R.id.name); name.setText(cursor.getString(cursor.getColumnIndex("name"))); TextView phone = (TextView) view.findViewById(R.id.phone); phone.setText(cursor.getString(cursor.getColumnIndex("phone"))); } } 

在Android中,如何在sqlite中使用带有原始查询的游标:

 Cursor c = sampleDB.rawQuery("SELECT FirstName, Age FROM mytable " + "where Age > 10 LIMIT 5", null); if (c != null ) { if (c.moveToFirst()) { do { String firstName = c.getString(c.getColumnIndex("FirstName")); int age = c.getInt(c.getColumnIndex("Age")); results.add("" + firstName + ",Age: " + age); }while (c.moveToNext()); } } c.close(); 

使用Sqlite的CursorAdapter示例

 ... DatabaseHelper helper = new DatabaseHelper(this); aListView = (ListView) findViewById(R.id.aListView); Cursor c = helper.getAllContacts(); CustomAdapter adapter = new CustomAdapter(this, c); aListView.setAdapter(adapter); ... class CustomAdapter extends CursorAdapter { // CursorAdapter will handle all the moveToFirst(), getCount() logic for you :) public CustomAdapter(Context context, Cursor c) { super(context, c); } public void bindView(View view, Context context, Cursor cursor) { String id = cursor.getString(0); String name = cursor.getString(1); // Get all the values // Use it however you need to TextView textView = (TextView) view; textView.setText(name); } public View newView(Context context, Cursor cursor, ViewGroup parent) { // Inflate your view here. TextView view = new TextView(context); return view; } } private final class DatabaseHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "db_name"; private static final int DATABASE_VERSION = 1; private static final String CREATE_TABLE_TIMELINE = "CREATE TABLE IF NOT EXISTS table_name (_id INTEGER PRIMARY KEY AUTOINCREMENT, name varchar);"; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_TABLE_TIMELINE); db.execSQL("INSERT INTO ddd (name) VALUES ('One')"); db.execSQL("INSERT INTO ddd (name) VALUES ('Two')"); db.execSQL("INSERT INTO ddd (name) VALUES ('Three')"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } public Cursor getAllContacts() { String selectQuery = "SELECT * FROM table_name; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); return cursor; } }