Search code examples
android-studioandroid-sqlitesimplecursoradapter

An error while populating ListView from SQL DB with SimpleCursorAdapter


So, im trying to write a program where i can create tasks, show their names and dates in listview and just simply edit them after clicking on them in listview

Im having some problems with populating. I need to use SimpleCursorAdapter for this program(as an assignment from my university)

This is my populate function which is inside my MainActivity

 private void populate(){
    Cursor cursor = myDb.getAllRows();
    String[] backDB = new String[] {DBAdapter.COLUMN_NAME, DBAdapter.COLUMN_DATE};
    int[] toView = new int[] {R.id.textViewName, R.id.textViewDate};
    SimpleCursorAdapter myCursor;
    myCursor = new SimpleCursorAdapter(getBaseContext(), R.layout.row_layout, cursor, backDB,toView, 0);
    ListView myList = (ListView) findViewById(R.id.listViewTasks);
    myList.setAdapter(myCursor);
}

i am thinking it might be related to sql database, especially getAllRows function because in logcat i can see there is a problem with the line:

 myCursor = new SimpleCursorAdapter(getBaseContext(), R.layout.row_layout, cursor, backDB,toView, 0); 

So, here is my getAllRows function

 public Cursor getAllRows() {
    String query = "SELECT * FROM " + TABLE_NAME;
    Cursor c = db.rawQuery(query, null);
    c.moveToFirst();
    return c;
}

My program just crashes and keeps crashing btw.


Solution

  • At a guess your issue is due to you not having a column named _id, in which case the log would contain something along the lines of :-

    06-07 10:53:53.957 1178-1178/so50635292.so50635292 E/AndroidRuntime: FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{so50635292.so50635292/so50635292.so50635292.MainActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
         Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
            at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:302)
            at android.widget.CursorAdapter.init(CursorAdapter.java:168)
            at android.widget.CursorAdapter.<init>(CursorAdapter.java:145)
            at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:91)
            at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:104)
            at so50635292.so50635292.MainActivity.populate(MainActivity.java:31)
            at so50635292.so50635292.MainActivity.onCreate(MainActivity.java:23)
            at android.app.Activity.performCreate(Activity.java:5008)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
            at android.app.ActivityThread.access$600(ActivityThread.java:130) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
            at android.os.Handler.dispatchMessage(Handler.java:99) 
            at android.os.Looper.loop(Looper.java:137) 
            at android.app.ActivityThread.main(ActivityThread.java:4745) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:511) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
            at dalvik.system.NativeStart.main(Native Method) 
    

    The relevant part being java.lang.IllegalArgumentException: column '_id' does not exist

    Cursor Adapters require this column and it should also be an alias of the rowid. An alias of the row is a column that is is defined as ?? INTEGER PRIMARY KEY, the optional, following, AUTOINCREMENT keyword can also be coded. However, generally it should not as there are overheads to coding AUTO INCREMENT.

    To fix the issue you could add the column to the table create sql in the onCreate method of DBAdapter and then deleting the App's data or uninstalling the App, and then rerunning the App.

    You could alternatively create an alias when querying the data by changing

     String query = "SELECT * FROM " + TABLE_NAME;
    

    to

    String query = "SELECT rowid AS _id,* FROM " + TABLE_NAME;
    
    • Note the following assumes that you have not coded WITHOUT ROWID