所以,我试着编写一个程序,我可以创建任务,在listview中显示他们的名字和日期,只需在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;
}
我的程序崩溃并且一直崩溃。
答案 0 :(得分:0)
猜测您的问题是由于您没有名为 _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而存在开销。
要解决此问题,您可以将列添加到表中,在DBAdapter的onCreate
方法中创建sql,然后删除App的数据或卸载App,然后重新运行App。
您也可以通过更改
查询数据时创建别名 String query = "SELECT * FROM " + TABLE_NAME;
到
String query = "SELECT rowid AS _id,* FROM " + TABLE_NAME;