使用 SimpleCursorAdapter 从 SQL DB 填充 ListView 时出错
An error while populating ListView from SQL DB with SimpleCursorAdapter
所以,我正在尝试编写一个程序,我可以在其中创建任务,在列表视图中显示它们的名称和日期,然后在列表视图中单击它们后只需简单地编辑它们
我在填充时遇到了一些问题。我需要为此程序使用 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;
}
我的程序刚刚崩溃,顺便说一句,一直在崩溃。
猜测 你的问题是由于你没有名为 _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.access0(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.access0(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 关键字进行编码。但是,通常不应该,因为编码自动递增会产生开销。
要解决此问题,您可以在 DBAdapter 的 onCreate
方法中将列添加到 table 创建 sql,然后删除应用程序的数据或卸载应用程序,然后重新运行应用程序。
您也可以通过更改
在查询数据时创建别名
String query = "SELECT * FROM " + TABLE_NAME;
到
String query = "SELECT rowid AS _id,* FROM " + TABLE_NAME;
- 注意以下假设你没有编码WITHOUT ROWID
所以,我正在尝试编写一个程序,我可以在其中创建任务,在列表视图中显示它们的名称和日期,然后在列表视图中单击它们后只需简单地编辑它们
我在填充时遇到了一些问题。我需要为此程序使用 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;
}
我的程序刚刚崩溃,顺便说一句,一直在崩溃。
猜测 你的问题是由于你没有名为 _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.access0(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.access0(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 关键字进行编码。但是,通常不应该,因为编码自动递增会产生开销。
要解决此问题,您可以在 DBAdapter 的 onCreate
方法中将列添加到 table 创建 sql,然后删除应用程序的数据或卸载应用程序,然后重新运行应用程序。
您也可以通过更改
在查询数据时创建别名 String query = "SELECT * FROM " + TABLE_NAME;
到
String query = "SELECT rowid AS _id,* FROM " + TABLE_NAME;
- 注意以下假设你没有编码WITHOUT ROWID