2012-08-02 235 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();