0

Cant Generate ListView With SimpleCursorAdapter this is my Cursor Method in Data Handler:

public Cursor getAllRows()
{
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(true,TABLE_ITEM,new String[]{KEY_NAME,KEY_VALUE},null,null,null,null,null,null);
    if(cursor != null)
    {
        cursor.moveToFirst();
    }
    return cursor;
}

and this is my list populating method in Activity:

public void makeList()
{
    Cursor cursor = db.getAllRows();
    String[] fromField = new String[]{"item_name", "item_name"};
    int[] toView = new int[]{R.id.tvName, R.id.tvVal};
    SimpleCursorAdapter sca = new SimpleCursorAdapter(this,R.layout.content_layout,cursor,fromField,toView,0);
    ListView lv = (ListView) findViewById(R.id.listView8);
    lv.setAdapter(sca);
}

and calling this method here:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_status);
    makeList();
}

after run it saying in emulator/device "Unfortunately stopped" and This are from log cat errors:

09-04 14:55:48.455  32680-32680/com.alright.plastic.dailyex E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.alright.plastic.dailyex, PID: 32680
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.alright.plastic.dailyex/com.alright.plastic.dailyex.StatusActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
        at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
        at android.support.v4.widget.CursorAdapter.init(CursorAdapter.java:174)
        at android.support.v4.widget.CursorAdapter.<init>(CursorAdapter.java:151)
        at android.support.v4.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:93)
        at android.support.v4.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:89)
        at com.alright.plastic.dailyex.StatusActivity.makeList(StatusActivity.java:109)
        at com.alright.plastic.dailyex.StatusActivity.onCreate(StatusActivity.java:26)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

For More Detail:git

alright
  • 59
  • 6

1 Answers1

0

The clue is at the top of your log cat errors: column '_id' does not exist. See Android column '_id' does not exist? for the solution. Basically, CursorAdapter requires the column _id in the results set to work so just add it to the list of columns. If your table does not have _id, either add it (easier in the long run) or use rawQuery to select your data, aliasing rowid as _id.

Community
  • 1
  • 1
NigelK
  • 8,255
  • 2
  • 30
  • 28