从Android中的光标获取数据

时间:2016-03-27 04:16:41

标签: java android

我的代码迭代光标

if (cursor.moveToFirst()) {                                                                 
while (cursor.moveToNext()) {                                                           
    String x  = cursor.getString(cursor.getColumnIndex("event_name"));                  
    System.out.println("Cursor ::::::" + x);                                            
    listViewModel.setEventDate(cursor.getString(cursor.getColumnIndex("event_date")));  
    modelList.add(listViewModel);                                                       
}                                                                                       

}

SYSO打印:

Cursor ::::::android.support.v7.widget.AppCompatEditText{2e1dacdf VFED..CL ........ 0,73-1344,209 #7f0c006f app:id/eventName} 

从Db获取数据的光标:

 public Cursor getEvents(Domain dbOperations){
    database = dbOperations.getReadableDatabase();
    String[] columns = {TableInfo.TableDetails.EVENT_NAME, TableInfo.TableDetails.EVENT_DATE};
    Cursor cursor = database.rawQuery("SELECT event_name, event_date FROM events", null);
    return cursor;
}

有人可以帮助我了解数据库中的确切内容吗? 感谢

1 个答案:

答案 0 :(得分:1)

当您插入数据库时​​:

contentValues.put("event_name", eventNameEditText.getText().toString());

你也应该改变循环:

if (cursor.moveToFirst()) {                                                                  
do {                                                            
    String x  = cursor.getString(cursor.getColumnIndex("event_name"));                  
    System.out.println("Cursor ::::::" + x);                                            
    listViewModel.setEventDate(cursor.getString(cursor.getColumnIndex("event_date")));   
    modelList.add(listViewModel);                                                        
}while (cursor.moveToNext());

因为当您调用moveToFirst()方法时,光标已准备好从第0行返回数据。当您立即致电moveToNext()时,它会转到第1行并跳过第一行。