使用游标获取列内容

时间:2013-09-11 05:31:23

标签: android list android-listview cursor android-cursoradapter

我有一个Cursor返回从SQLiteDataBase对象,我使用游标getXXX()方法从光标获取列的内容,但我收到错误。 我的代码是从数据库中获取光标并获取对象的地方......

public List<Message> getAllMessages(char ch) {
      Cursor cursor = null;
    List<Message> message = new ArrayList<Message>();
if(ch=='r')
    {
    String queryz = "SELECT " + MySQLiteHelper.COLUMN_ID + "," + MySQLiteHelper.COLUMN_MESSAGE + " FROM " + MySQLiteHelper.TABLE_NAME + " WHERE " +MySQLiteHelper.COLUMN_STATUS + "= 'r'";
cursor = database.rawQuery(queryz, null);
}
if(ch=='s')
{
String queryz = "SELECT " + MySQLiteHelper.COLUMN_ID + "," + MySQLiteHelper.COLUMN_MESSAGE + " FROM " + MySQLiteHelper.TABLE_NAME + " WHERE " +MySQLiteHelper.COLUMN_STATUS + "= 's'";
cursor = database.rawQuery(queryz, null);
}


   cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
      Message message1 = cursorToMessage(cursor);
      message.add(message1);
      cursor.moveToNext();
    }
    // Make sure to close the cursor
    cursor.close();
    return message;
  }

    public Message createMessage(String message,char ch,int random) {
        ContentValues values = new ContentValues();

        if(ch=='s')
        values.put(MySQLiteHelper.COLUMN_STATUS, "s");
    if(ch=='r')
        values.put(MySQLiteHelper.COLUMN_STATUS, "r");

        values.put(MySQLiteHelper.COLUMN_PARENTID, 0);

        values.put(MySQLiteHelper.COLUMN_THREADID, random);

        values.put(MySQLiteHelper.COLUMN_MESSAGE, message);
        long insertId = database.insert(MySQLiteHelper.TABLE_NAME, null,
            values);


       Cursor cursor = database.query(MySQLiteHelper.TABLE_NAME,
            allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
            null, null, null);
       /* Cursor cursor = database.query(MySQLiteHelper.TABLE_NAME,
                allColumns, null, null,
                null, null, null);*/
       cursor.moveToFirst();
        Message newMessage = cursorToMessage(cursor);
       cursor.close();
        return newMessage;
      }
         private Message cursorToMessage(Cursor cursor) {
            Message message = new Message();
            message.setId(cursor.getLong(cursor.getColumnIndex("id")));

            message.setmessage(cursor.getString(cursor.getColumnIndex("message")));
            message.setthreadid(cursor.getLong(cursor.getColumnIndex("threadid")));

            return message;
          }

    My log-cat is:

        09-11 17:15:01.227: E/AndroidRuntime(4707): FATAL EXCEPTION: main
        09-11 17:15:01.227: E/AndroidRuntime(4707): java.lang.RuntimeException: Unable to start activity ComponentInfo{soft.b.peopleassist/soft.b.peopleassist.MainActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
        09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
        09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
        09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
        09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
        09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.os.Handler.dispatchMessage(Handler.java:99)
        09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.os.Looper.loop(Looper.java:137)
        09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.app.ActivityThread.main(ActivityThread.java:4340)
        09-11 17:15:01.227: E/AndroidRuntime(4707):     at java.lang.reflect.Method.invokeNative(Native Method)
        09-11 17:15:01.227: E/AndroidRuntime(4707):     at java.lang.reflect.Method.invoke(Method.java:511)
        09-11 17:15:01.227: E/AndroidRuntime(4707):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
        09-11 17:15:01.227: E/AndroidRuntime(4707):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
        09-11 17:15:01.227: E/AndroidRuntime(4707):     at dalvik.system.NativeStart.main(Native Method)
        09-11 17:15:01.227: E/AndroidRuntime(4707): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
        09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.database.CursorWindow.nativeGetLong(Native Method)
        09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.database.CursorWindow.getLong(CursorWindow.java:526)
        09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:75)
        09-11 17:15:01.227: E/AndroidRuntime(4707):     at soft.b.peopleassist.DataSource.cursorToMessage(DataSource.java:104)
        09-11 17:15:01.227: E/AndroidRuntime(4707):     at soft.b.peopleassist.DataSource.getAllMessages(DataSource.java:89)
        09-11 17:15:01.227: E/AndroidRuntime(4707):     at soft.b.peopleassist.Send.onCreateView(Send.java:58)
        09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:795)
        09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:998)
        09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.app.BackStackRecord.run(BackStackRecord.java:622)
        09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1330)
        09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.app.Activity.performStart(Activity.java:4474)
        09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1928)

3 个答案:

答案 0 :(得分:1)

需要两次修改。添加

cursor.moveToFirst();

cursor.close();

进入cursorToMessage()


我做了一些修改。使用以下代码

// cursor.moveToFirst(); // No need to move position
    Message newMessage = cursorToMessage(cursor);
    // cursor.close(); // No need to close here

     private Message cursorToMessage(Cursor cursor) {
         if (cursor != null && cursor.moveToFirst()) {
             Message message = new Message();
             message.setId(cursor.getLong(cursor.getColumnIndex("id")));
             message.setmessage(cursor.getString(cursor.getColumnIndex("message")));
             message.setthreadid(cursor.getLong(cursor.getColumnIndex("threadid")));
             cursor.close(); 
             return message;
         }
        return null;
      }

确保您正在将所有列 id, message, threadid 从数据库中读取到Cursor中。确保您可以看到 allColumns 的价值。

答案 1 :(得分:0)

小错误

cursor.moveToFirst();
   Message newMessage = cursorToMessage(cursor);

     // UPDATE HERE Remove cursor.close();
        cursor.close(); //Do not close cursor here

 private Message cursorToMessage(Cursor cursor) {
    Message message = new Message();
    message.setId(cursor.getLong(cursor.getColumnIndex("id")));

    message.setmessage(cursor.getString(cursor.getColumnIndex("message")));
    message.setthreadid(cursor.getLong(cursor.getColumnIndex("threadid")));

cursor.close(); //UPDATE HERE
    return message;
  }

答案 2 :(得分:0)

请检查这些列是否在同一个案例的表格中。您使用的查询应具有相同的列名称和相同的大小写。当其中一个不匹配发生时,我已经看到了这个问题....