Search code examples
androidsqlitelistviewandroid-cursoradapter

Displaying items in listView from sqlite database (Android)


I am trying to populate a list view on android. I've written the code and it runs without errors, however it crashes when i try to run it. Below is the Populate list view method:

 private void populateListView() {
    Cursor cursor = db.getAllRows();
    String[] fromFieldNames = new String[]{MySQLiteHelper.KEY_TITLE, MySQLiteHelper.KEY_AUTHOR};
    int[] toViewIDs = new int[]{R.id.textView2, R.id.textView3};
    SimpleCursorAdapter myCursorAdapter;
    myCursorAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.itemlayout,cursor,fromFieldNames,toViewIDs,0);
    listView.setAdapter(myCursorAdapter);
}

Here is the getAllRows function from my SQLiteHelper class

 public Cursor getAllRows()
{
    SQLiteDatabase db = this.getReadableDatabase();
    String query = "SELECT * FROM " + TABLE_BOOKS;
    Cursor cursor = db.rawQuery(query, null);
    if(cursor!=null)
    {
       cursor.moveToFirst();
    }
   return cursor;
}

From the crash report, the error seems to be from :

 myCursorAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.itemlayout,cursor,fromFieldNames,toViewIDs,0);

The error log is:

05-02 05:07:15.395    1882-1882/com.example.user.databasetutorial I/art﹕ Not late-enabling -Xcheck:jni (already on)
05-02 05:07:15.571    1882-1894/com.example.user.databasetutorial I/art﹕ WaitForGcToComplete blocked for 31.000ms for cause Background
05-02 05:07:15.720    1882-1894/com.example.user.databasetutorial W/art﹕ Suspending all threads took: 19.028ms
05-02 05:07:15.726    1882-1894/com.example.user.databasetutorial I/art﹕ Background partial concurrent mark sweep GC freed 269(47KB) AllocSpace objects, 0(0B) LOS objects, 47% free, 562KB/1074KB, paused 20.265ms total 77.611ms
05-02 05:07:16.035    1882-1882/com.example.user.databasetutorial D/AndroidRuntime﹕ Shutting down VM
05-02 05:07:16.035    1882-1882/com.example.user.databasetutorial E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.user.databasetutorial, PID: 1882
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.databasetutorial/com.example.user.databasetutorial.MainActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
            at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
            at android.widget.CursorAdapter.init(CursorAdapter.java:172)
            at android.widget.CursorAdapter.<init>(CursorAdapter.java:149)
            at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:91)
            at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:104)
            at com.example.user.databasetutorial.MainActivity.populateListView(MainActivity.java:108)
            at com.example.user.databasetutorial.MainActivity.onCreate(MainActivity.java:53)
            at android.app.Activity.performCreate(Activity.java:5937)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

I cannot seem to figure out what the problem is. Thank you


Solution

  • The Cursor must include a column named "_id" or this class will not work.

    Modify your select query as

    String query = "Select rowid as _id,title,author from " + TABLE_BOOKS;