android.database.CursorIndexOutOfBoundsException

后端 未结 3 1334
再見小時候
再見小時候 2021-02-14 15:21

The data access code in my Android app raises an exception.

Here is the code that causes the exception:

String getPost = \"SELECT * FROM \" + TABLE_POST          


        
3条回答
  •  没有蜡笔的小新
    2021-02-14 15:48

    You havent moved cursor index to first .. Try like this below

     String getPost = "SELECT * FROM " + TABLE_POST + ";";
        Cursor result = getReadableDatabase().rawQuery(getPost, null);
        List posts = new ArrayList();{
        Log.d("count", result.getCount() + " post rows");
    
        if (result .moveToFirst()) {
            do {
                 Post post = new Post();
                 post.setPostId(result.getInt(0));
                 posts.add(post);
                  ....
            } while (result .moveToNext());
        }
        result .close();
    

提交回复
热议问题