如何使用android中的游标适配器从sqlite数据库检索数据到自定义列表视图

时间:2012-05-14 11:28:24

标签: android sqlite simplecursoradapter android-cursoradapter

我在listview中从sqlite数据库获取数据到Android移动设备。数据在列表中逐一显示。我将使用游标适配器在自定义列表视图中显示两个textview。 的 Main.java

public class Main extends Activity  {

    private SQLiteAdapter mySQLiteAdapter;
    /** Called when the activity is first created. */


        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            ListView listContent = (ListView) findViewById(R.id.customlist);

            /*
             * Create/Open a SQLite database and fill with dummy content and close
             * it
             */
            mySQLiteAdapter = new SQLiteAdapter(this);
            mySQLiteAdapter.openToWrite();
            // mySQLiteAdapter.deleteAll();
            mySQLiteAdapter = new SQLiteAdapter(this);
            mySQLiteAdapter.openToRead();

            final Cursor cursor = mySQLiteAdapter.queueAll();
            startManagingCursor(cursor);

            String[] from = new String[] { SQLiteAdapter.KEY_CONTENT };
            int[] to = new int[] { R.id.text };

            final SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
                    R.layout.row, cursor, from, to);

            listContent.setAdapter(cursorAdapter);

            mySQLiteAdapter.close();
            }
    }

SQLiteAdapter.java

public class SQLiteAdapter {

    public static final String MYDATABASE_NAME = "test";
    public static final String MYDATABASE_TABLE = "test";
    public static final int MYDATABASE_VERSION = 1;     
    public static final String KEY_ID = "_id";
    public static final String KEY_CONTENT = "test"


    // create table MY_DATABASE (ID integer primary key, Content text not null);
    private static final String SCRIPT_CREATE_DATABASE = "create table test (_id integer primary key autoincrement, "
             + "test text not null)";

    private SQLiteHelper sqLiteHelper;
    private SQLiteDatabase sqLiteDatabase;

    private Context context;

    public SQLiteAdapter(Context c) {
        context = c;
    }

    public SQLiteAdapter openToRead() throws android.database.SQLException {
        sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null,
                MYDATABASE_VERSION);
        sqLiteDatabase = sqLiteHelper.getReadableDatabase();
        return this;
    }

    public SQLiteAdapter openToWrite() throws android.database.SQLException {
        sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null,
                MYDATABASE_VERSION);
        sqLiteDatabase = sqLiteHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        sqLiteHelper.close();
    }


    public int deleteAll() {
        return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
    }

    public Cursor queueAll() {
        String[] columns = new String[] { KEY_ID, KEY_CONTENT };
        Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, null,
                null, null, null, null);

        int id=cursor.getColumnIndex(KEY_CONTENT);
        cursor.moveToFirst();
        if(cursor!=null)
        {
            do      
            {
                String keyid=cursor.getString(id);
                System.out.println(keyid);
            }while(cursor.moveToNext());
        }

        return cursor;
    }

    private static class SQLiteHelper extends SQLiteOpenHelper {

        public SQLiteHelper(Context context, String name,
                CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL(SCRIPT_CREATE_DATABASE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub

        }
    }
    }

使用上面的代码我可以在listview中显示key_content。现在,我将使用游标适配器在listview中显示key_id和key_content。

1 个答案:

答案 0 :(得分:1)

 Cursor cursor = mySQLiteAdapter.queueAll();
queueAll()方法

中的

 Cursror c = sqLiteDatabase.rawQuery("select * from TABLENAME",null);

 return c;

而不是*,您可以添加表格列名称,如key_id,key_content

获取id和名称

String myContent = cursor.getString(cursor.getColumnIndex("key_content")).toString().trim();

    long id = cursor.getLong(cursor.getColumnIndex("key_id"));
相关问题