3

I have an SQLiteDatabase helper, which returns a cursor with just the name column from a database:

public Cursor getNames() {
    Cursor cursor = db.query(TABLE_NAME, new String[] {NAME}, null, null, null, null, null);
    return cursor;
}

I am trying to bind this cursor to a simple ListView contained in the Layout:

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

The java:

 Cursor cursor = db.getNames();
    startManagingCursor(cursor);

    ListAdapter adapter = new SimpleCursorAdapter (
            this,
            android.R.layout.simple_list_item_1,
            cursor,
            new String[] {constants.NAME},
            new int[] {android.R.id.text1}
            );

    setListAdapter(adapter);

I've followed every tutorial I can find but the app still stops unexpectedly. Please tell me what I'm doing wrong!

Here's the LOGCAT trace, hope thats what you needed:

12-03 17:18:41.557: E/AndroidRuntime(30413): FATAL EXCEPTION: main
12-03 17:18:41.557: E/AndroidRuntime(30413): java.lang.RuntimeException: Unable to start activity ComponentInfo{george.frost.YourCarbDatabase/com.android.CarbCount.Search}: java.lang.IllegalArgumentException: column '_id' does not exist
12-03 17:18:41.557: E/AndroidRuntime(30413):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
12-03 17:18:41.557: E/AndroidRuntime(30413):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
12-03 17:18:41.557: E/AndroidRuntime(30413):    at android.app.ActivityThread.access$1500(ActivityThread.java:121)
12-03 17:18:41.557: E/AndroidRuntime(30413):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
12-03 17:18:41.557: E/AndroidRuntime(30413):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-03 17:18:41.557: E/AndroidRuntime(30413):    at android.os.Looper.loop(Looper.java:130)
12-03 17:18:41.557: E/AndroidRuntime(30413):    at android.app.ActivityThread.main(ActivityThread.java:3701)
12-03 17:18:41.557: E/AndroidRuntime(30413):    at java.lang.reflect.Method.invokeNative(Native Method)
12-03 17:18:41.557: E/AndroidRuntime(30413):    at java.lang.reflect.Method.invoke(Method.java:507)
12-03 17:18:41.557: E/AndroidRuntime(30413):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
12-03 17:18:41.557: E/AndroidRuntime(30413):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
12-03 17:18:41.557: E/AndroidRuntime(30413):    at dalvik.system.NativeStart.main(Native Method)
12-03 17:18:41.557: E/AndroidRuntime(30413): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
12-03 17:18:41.557: E/AndroidRuntime(30413):    at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
12-03 17:18:41.557: E/AndroidRuntime(30413):    at android.widget.CursorAdapter.init(CursorAdapter.java:111)
12-03 17:18:41.557: E/AndroidRuntime(30413):    at android.widget.CursorAdapter.<init>(CursorAdapter.java:90)
12-03 17:18:41.557: E/AndroidRuntime(30413):    at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:47)
12-03 17:18:41.557: E/AndroidRuntime(30413):    at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:84)
12-03 17:18:41.557: E/AndroidRuntime(30413):    at com.android.CarbCount.Search.onCreate(Search.java:42)
12-03 17:18:41.557: E/AndroidRuntime(30413):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-03 17:18:41.557: E/AndroidRuntime(30413):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
12-03 17:18:41.557: E/AndroidRuntime(30413):    ... 11 more
1
  • Can you post the stack trace along with the code from startManagingCursor? Commented Dec 3, 2011 at 17:08

2 Answers 2

2

The startManagingCursor()-method is deprecated. You should use the Loader API instead. If you're targeting devices with an API-Level lower then 11, you'll want to use the compatibility library.

Since it seams that you simply want to load some data from your SQLiteDatabase, you don't need to create a ContentProvider but can extend Loader. An example and more information on this can be found here: CursorLoader usage without ContentProvider

Also, if your Activity only shows a ListView, you might want to switch to a ListActivity, since it makes binding data and getting ID's (for example) easier for you.


Since you posted your LogCat:

The problem is, that the SimpleCursorAdapter-class needs a column named _id to get an ID on every row of data from your Database. More information on that problem can be found here: Android column '_id' does not exist?

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I did not know startManagingCursor() method was depreciated. I have extended ListActivity although my layout contains a button at the top and a listview underneath. Yes I just want to load one column of data and display it in the listview
1

Does ur table contains column with primary key... first line of logcat says....

12-03 17:18:41.557: E/AndroidRuntime(30413): java.lang.RuntimeException: Unable to start activity ComponentInfo{george.frost.YourCarbDatabase/com.android.CarbCount.Search}: java.lang.IllegalArgumentException: column '_id' does not exist  

see this....

 column '_id' does not exist

1 Comment

Cursor identifies entries by using _id so tables in sqlite must contain this,if u are using sumthing like this....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.