首页 > 解决方案 > 使用 SimpleCursorAdapter 从 SQL DB 填充 ListView 时出错

问题描述

所以,我试图编写一个程序,我可以在其中创建任务,在列表视图中显示它们的名称和日期,然后在列表视图中单击它们后简单地编辑它们

我在填充时遇到了一些问题。我需要为这个程序使用 SimpleCursorAdapter(作为我大学的作业)

这是我的 MainActivity 中的填充函数

 private void populate(){
    Cursor cursor = myDb.getAllRows();
    String[] backDB = new String[] {DBAdapter.COLUMN_NAME, DBAdapter.COLUMN_DATE};
    int[] toView = new int[] {R.id.textViewName, R.id.textViewDate};
    SimpleCursorAdapter myCursor;
    myCursor = new SimpleCursorAdapter(getBaseContext(), R.layout.row_layout, cursor, backDB,toView, 0);
    ListView myList = (ListView) findViewById(R.id.listViewTasks);
    myList.setAdapter(myCursor);
}

我认为它可能与 sql 数据库有关,尤其是 getAllRows 函数,因为在 logcat 中我可以看到该行存在问题:

 myCursor = new SimpleCursorAdapter(getBaseContext(), R.layout.row_layout, cursor, backDB,toView, 0); 

所以,这是我的 getAllRows 函数

 public Cursor getAllRows() {
    String query = "SELECT * FROM " + TABLE_NAME;
    Cursor c = db.rawQuery(query, null);
    c.moveToFirst();
    return c;
}

我的程序只是崩溃并不断崩溃。

标签: android-studioandroid-sqlitesimplecursoradapter

解决方案


猜测您的问题是由于您没有名为_id的列,在这种情况下,日志将包含以下内容:-

06-07 10:53:53.957 1178-1178/so50635292.so50635292 E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{so50635292.so50635292/so50635292.so50635292.MainActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
        at android.app.ActivityThread.access$600(ActivityThread.java:130)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4745)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
        at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:302)
        at android.widget.CursorAdapter.init(CursorAdapter.java:168)
        at android.widget.CursorAdapter.<init>(CursorAdapter.java:145)
        at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:91)
        at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:104)
        at so50635292.so50635292.MainActivity.populate(MainActivity.java:31)
        at so50635292.so50635292.MainActivity.onCreate(MainActivity.java:23)
        at android.app.Activity.performCreate(Activity.java:5008)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
        at android.app.ActivityThread.access$600(ActivityThread.java:130) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:137) 
        at android.app.ActivityThread.main(ActivityThread.java:4745) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:511) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
        at dalvik.system.NativeStart.main(Native Method) 

相关部分是java.lang.IllegalArgumentException: column '_id' does not exist

光标适配器需要此列,它也应该是 rowid 的别名。行的别名是定义为 的列,也可以?? INTEGER PRIMARY KEY对可选的、后续的 AUTOINCREMENT 关键字进行编码。但是,通常不应该这样做,因为编码 AUTO INCREMENT 会产生开销。

onCreate要解决此问题,您可以在DBAdapter的方法中将列添加到表中创建 sql,然后删除应用程序的数据或卸载应用程序,然后重新运行应用程序。

您也可以在查询数据时通过更改创建别名

 String query = "SELECT * FROM " + TABLE_NAME;

String query = "SELECT rowid AS _id,* FROM " + TABLE_NAME;
  • 请注意以下假设您没有编码 WITHOUT ROWID

推荐阅读