在sqlite数据库(Android)中显示listView中的项目

时间:2015-04-30 21:30:37

标签: android sqlite listview android-cursoradapter

我正在尝试在android上填充列表视图。我已经编写了代码并且运行时没有错误,但是当我尝试运行它时它会崩溃。以下是填充列表视图方法:

 private void populateListView() {
    Cursor cursor = db.getAllRows();
    String[] fromFieldNames = new String[]{MySQLiteHelper.KEY_TITLE, MySQLiteHelper.KEY_AUTHOR};
    int[] toViewIDs = new int[]{R.id.textView2, R.id.textView3};
    SimpleCursorAdapter myCursorAdapter;
    myCursorAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.itemlayout,cursor,fromFieldNames,toViewIDs,0);
    listView.setAdapter(myCursorAdapter);
}

这是我的SQLiteHelper类的

中的getAllRows函数
 public Cursor getAllRows()
{
    SQLiteDatabase db = this.getReadableDatabase();
    String query = "SELECT * FROM " + TABLE_BOOKS;
    Cursor cursor = db.rawQuery(query, null);
    if(cursor!=null)
    {
       cursor.moveToFirst();
    }
   return cursor;
}

从崩溃报告中,错误似乎来自:

 myCursorAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.itemlayout,cursor,fromFieldNames,toViewIDs,0);

错误日志是:

05-02 05:07:15.395    1882-1882/com.example.user.databasetutorial I/art﹕ Not late-enabling -Xcheck:jni (already on)
05-02 05:07:15.571    1882-1894/com.example.user.databasetutorial I/art﹕ WaitForGcToComplete blocked for 31.000ms for cause Background
05-02 05:07:15.720    1882-1894/com.example.user.databasetutorial W/art﹕ Suspending all threads took: 19.028ms
05-02 05:07:15.726    1882-1894/com.example.user.databasetutorial I/art﹕ Background partial concurrent mark sweep GC freed 269(47KB) AllocSpace objects, 0(0B) LOS objects, 47% free, 562KB/1074KB, paused 20.265ms total 77.611ms
05-02 05:07:16.035    1882-1882/com.example.user.databasetutorial D/AndroidRuntime﹕ Shutting down VM
05-02 05:07:16.035    1882-1882/com.example.user.databasetutorial E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.user.databasetutorial, PID: 1882
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.databasetutorial/com.example.user.databasetutorial.MainActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: 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:149)
            at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:91)
            at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:104)
            at com.example.user.databasetutorial.MainActivity.populateListView(MainActivity.java:108)
            at com.example.user.databasetutorial.MainActivity.onCreate(MainActivity.java:53)
            at android.app.Activity.performCreate(Activity.java:5937)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我似乎无法弄清问题是什么。谢谢

0 个答案:

没有答案