2

我正在列表视图中从 sqlite 数据库中获取数据到 Android 移动设备。数据在列表中一一出现。我将使用光标适配器在自定义列表视图中显示两个文本视图。 主.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

        }
    }
    }

使用上面的代码,我可以在列表视图中显示 key_content。现在我将使用光标适配器在列表视图中显示 key_id 和 key_content。

4

1 回答 1

1
 Cursor cursor = mySQLiteAdapter.queueAll();

queueAll()方法中

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

 return c;

而不是*你可以添加你的表格列名,比如key_id,key_content

并获取 id 和 name

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

    long id = cursor.getLong(cursor.getColumnIndex("key_id"));
于 2012-05-14T11:34:48.320 回答