2012-08-02 180 views
1

如何從sqlite數據庫從適配器類中檢索數據並插入到列表視圖?從SQLite數據庫中檢索數據

我有困難,一直在編碼日&夜間和谷歌搜索不停,但我只是不明白的鏈接,當我打開它們。我剛開始學習編程機器人最近在我自己...

我在下方附上

public Cursor RetrieveActivityCursor(String NRIC){ 
    String where = NRIC; 

Cursor cursor = _db.query(DATABASE_TABLE,new String[]{ MOBILE_HOUSE_VISIT_ID, ELDERLY_NAME, 
     ELDERLY_NRIC, ELDERLY_DATE_TIME, PHYSICAL_HEALTH_STATUS, MENTAL_HEALTH_STATUS} , where, null, null, null, null); 
//Cursor cursor = _db.query(DATABASE_TABLE, new String[] { ELDERLY_NAME, 
     //ELDERLY_NRIC, DATE_TIME, PHYSICAL_HEALTH_STATUS, MENTAL_HEALTH_STATUS }, null, null, null, null, null); 
return cursor; 
} 

這個代碼是從我的適配器類我的代碼,並有身份證來傳遞和返回插入ListView的日期時間值

我不太清楚如何編寫代碼來調用這個方法。

回答

1

如果要擴展的數據庫處理的SqliteOpenHelper並使用另一個類延伸活動,在其中你有你的ListView那麼,在你的Activity類使你的班級的對象喜歡,

YourDBClass helperDB; 
helperDB = new HelperDB(YourActivityClass.this); 

並從數據庫中檢索數據。

做出這樣光標參考,

Cursor cursor; 

,然後執行這個樣子,

cursor = helperDB.RetrieveActivityCursor(NRIC); 
cursor.moveToFirst(); 
while(!cursor.isAfterLast()) { 
    // here you have to collect the data in the collection object. 
    cursor.moveToNext(); 
} 
cursor.close(); 

這就是它!你已經完成了從數據庫中獲取數據的操作

0

你需要迭代光標,就像這樣:

Cursor cursor = RetrieveActivityCursor(NRIC); 
if (cursor != null && cursor.moveToNext()) 
do { 
    String str = cursor.getString(cursor.getColumnIndex("your_column")); 

    // do something, your code 

} while (cursor.moveToNext()); 
0

如果你想檢索日期值,請在那裏進行下面的操作。

Cursor cursor = RetrieveActivityCursor(NRIC); 
if(mCursor.moveToFirst()) {    
    do{ 
     String date_time = cursor.getString(cursor.getColumnIndex(column_name)); 
    } while(mCursor.moveToNext()); 
} 
cursor.close();