Android,将ListView绑定到SQLite数据库

时间:2011-12-03 17:04:46

标签: java android sqlite listview

我有一个SQLiteDatabase帮助器,它从数据库返回一个只带有name列的游标:

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

我正在尝试将此光标绑定到Layout中包含的简单ListView:

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

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);

我已经按照我能找到的每个教程,但应用程序仍然意外停止。请告诉我我做错了什么!

这是LOGCAT跟踪,希望这就是你所需要的:

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

2 个答案:

答案 0 :(得分:2)

不推荐startManagingCursor() - 方法。您应该使用Loader API代替。如果您要定位API级别低于11的设备,则需要使用compatibility library

由于它只是想从SQLiteDatabase加载一些数据,因此您不需要创建ContentProvider但可以扩展Loader。有关此问题的示例和更多信息,请访问:CursorLoader usage without ContentProvider

此外,如果您的活动仅显示ListView,您可能需要切换到ListActivity,因为它可以让您更轻松地生成绑定数据并获取ID(例如)。


自从你发布了LogCat:

问题是,SimpleCursorAdapter - 类需要一个名为_id的列来获取数据库中每行数据的ID。有关该问题的更多信息,请访问:Android column '_id' does not exist?

答案 1 :(得分:1)

你的表是否包含带主键的列... logcat的第一行说....

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  

看到这个......

 column '_id' does not exist