游标适配器和sqlite示例

时间:2011-03-28 10:32:39

标签: android database sqlite android-layout android-cursoradapter

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

3 个答案:

答案 0 :(得分:103)

非常简单的例子。

这是一个非常简单但非常有效的例子。一旦你掌握了基础知识,你就可以轻松地建立它。

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

  1. 从数据库中创建正确的光标

  2. 创建一个自定义光标适配器,它从数据库中获取光标数据并将其与您想要表示的视图配对数据用。

  3. 1。从数据库中创建一个正确的光标。

    在您的活动中:

    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。创建自定义光标适配器。

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

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

答案 1 :(得分:37)

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

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

答案 2 :(得分:20)

使用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;
    }
}