-1

I am trying to display data from an SQLite database in a ListView. I use a CursorAdapter to directly plug the Cursor into the ListView but for some reason I am always getting an exception. This is my modified DBadapter activity:

public static final String ID="_id";

    public Cursor feching_Data(){

            String[] columns = {ID,USER_NAME,USER_PASSWORD};
            db = dbHelper.getWritableDatabase();
            Cursor cursor = db.query(TABLE_NAME, columns,null,null,null,null,null);
            return cursor;

        }

Here I am setting the SimpleCursorAdapter:

public static final String ID="_id";
    String[] from = {logindatabase_adapter.USER_NAME,logindatabase_adapter.USER_PASSWORD};
    int[] to = {R.id.txt_username,R.id.txt_pasword};
    Cursor cursor = logindatabase_adapter.feching_Data();
    cursoradapter = new SimpleCursorAdapter(DatabaseListView.this, R.layout.listcell, cursor, from, to);
    database_results.setAdapter(cursoradapter);

And this is the exception I am getting:

FATAL EXCEPTION: main
   Process: com.developer.milanandroid, PID: 5109
   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:120)
    at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:52)
    at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:78)
    at com.developer.milanandroid.DatabaseListView$1.onClick(DatabaseListView.java:50)
    at android.view.View.performClick(View.java:4438)
    at android.view.View.onKeyUp(View.java:8241)
    at android.widget.TextView.onKeyUp(TextView.java:5682)
    at android.view.KeyEvent.dispatch(KeyEvent.java:2664)
    at android.view.View.dispatchKeyEvent(View.java:7665)
    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2035)
    at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1505)
    at android.app.Activity.dispatchKeyEvent(Activity.java:2418)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1962)
    at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3852)
    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3826)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3558)
    at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3718)
    at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2010)
    at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1704)
    at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1695)
    at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1987)
    at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
    at android.os.MessageQueue.nativePollOnce(Native Method)
    at android.os.MessageQueue.next(MessageQueue.java:138)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    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:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)
dEePU
  • 195
  • 5
  • 18

2 Answers2

0

Just look at the logcat, the source of the error is explained right there in the second line:

java.lang.IllegalArgumentException: column '_id' does not exist.

Your table does not have an _id column, but CursorAdapter require the presence of an _id column (as explained in the docs right here).


The solution in your case is simple: Your already have an id column but it's called ID. Rename it to _id and everything should work fine!

Note hat you probably have to reinstall your app for changes to columns and/or tables to take effect!

Xaver Kapeller
  • 49,491
  • 11
  • 98
  • 86
  • i replaced my ID to _id but it does not works dude.again occurs same please help me dude @Xaver Kapeller – dEePU May 05 '15 at 12:53
  • Have you reinstalled the app? And with reinstall I mean completely deinstalled it from the device and then deployed it again? – Xaver Kapeller May 05 '15 at 12:54
  • Nope dude i just deployed it again only i'll check it by uninstalling – dEePU May 05 '15 at 12:57
  • Well are you sure it's the same error? And are you sure you really did fix it? – Xaver Kapeller May 05 '15 at 12:58
  • I can guarantee you that if that table has an `_id` column then it will work. You must be doing something else wrong. Recheck your implementation. – Xaver Kapeller May 05 '15 at 12:58
  • Yup, dude replacing with _id was works fine and i am not seeing list view after clicking button.I posted modified activity code above please check it – dEePU May 05 '15 at 13:03
  • @lakshmansundeep Don't do that. If you have a new problem then ask a new question. One question for each issue. If this issue - the one with `_id` - is solved then accept the answer which helped you most, also vote if you can, and then go ask a new question. – Xaver Kapeller May 05 '15 at 13:05
  • But i am not having such rating on my account dude,So that's why i asked you. Thanks for patiently answering and for your valuable answer @Xaver Kapeller – dEePU May 05 '15 at 13:10
0

Instead of renaming to _id the other way is that , in query you can select ID as _id

Its been already answered in SO.

  1. Android: column '_id' does not exist
  2. Android column '_id' does not exist?
  3. About “_id” field in Android SQLite
Community
  • 1
  • 1
Bharatesh
  • 8,943
  • 3
  • 38
  • 67
  • If you write an answer it has to be completely self contained. Now in context the text "Instead of renaming to `_id`..." makes sense, but people looking for answer to the question will not understand that. They are just going to think "rename what?". – Xaver Kapeller May 05 '15 at 12:56