我正在尝试从原始查询语句更新列表视图,但是我遇到了一些错误。
我在databaseAdapter类中有我的方法:
public Cursor getOpenLockers() {
Cursor c = ourDatabase.rawQuery("SELECT number, status FROM "
+ DATABASE_TABLE + " WHERE status = 'open'", null);
if (c != null) {
c.moveToFirst();
}
return c;
}
然后我有了listview方法:
private void loadListOpenLockers() {
// TODO Auto-generated method stub
Locker getList = new Locker(this);
getList.open();
Cursor cursor = getList.getOpenLockers();
startManagingCursor(cursor);
// setup mapping
String[] fromFieldNames = new String[] { getList.KEY_LOCKERNUMBER,
getList.KEY_STATUS };
int[] toViewIDs = new int[] { R.id.tvLockerNumber, R.id.tvSatus };
// create adapter
SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this,
R.layout.itemlayout, cursor, fromFieldNames, toViewIDs);
// set the adapter
ListView myList = (ListView) findViewById(R.id.lvInfo);
myList.setAdapter(myCursorAdapter);
}
然后我从onClickListener调用该方法,我在logcat中得到以下内容:
> 04-03 21:35:07.695: E/AndroidRuntime(29384): FATAL EXCEPTION: main
> 04-03 21:35:07.695: E/AndroidRuntime(29384): Process:
> com.********, PID: 29384 04-03 21:35:07.695:
> E/AndroidRuntime(29384): java.lang.IllegalArgumentException: column
> '_id' does not exist 04-03 21:35:07.695: E/AndroidRuntime(29384): at
> android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:309)
> 04-03 21:35:07.695: E/AndroidRuntime(29384): at
> android.widget.CursorAdapter.init(CursorAdapter.java:172) 04-03
> 21:35:07.695: E/AndroidRuntime(29384): at
> android.widget.CursorAdapter.<init>(CursorAdapter.java:120) 04-03
> 21:35:07.695: E/AndroidRuntime(29384): at
> android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:52)
> 04-03 21:35:07.695: E/AndroidRuntime(29384): at
> android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:78)
> 04-03 21:35:07.695: E/AndroidRuntime(29384): at
> com.samsunglockercenter.MainActivity.loadListOpenLockers(MainActivity.java:130)
> 04-03 21:35:07.695: E/AndroidRuntime(29384): at
> com.samsunglockercenter.MainActivity.onClick(MainActivity.java:161)
> 04-03 21:35:07.695: E/AndroidRuntime(29384): at
> android.view.View.performClick(View.java:4480) 04-03 21:35:07.695:
> E/AndroidRuntime(29384): at
> android.view.View$PerformClick.run(View.java:18673) 04-03
> 21:35:07.695: E/AndroidRuntime(29384): at
> android.os.Handler.handleCallback(Handler.java:733) 04-03
> 21:35:07.695: E/AndroidRuntime(29384): at
> android.os.Handler.dispatchMessage(Handler.java:95) 04-03
> 21:35:07.695: E/AndroidRuntime(29384): at
> android.os.Looper.loop(Looper.java:157) 04-03 21:35:07.695:
> E/AndroidRuntime(29384): at
> android.app.ActivityThread.main(ActivityThread.java:5872) 04-03
> 21:35:07.695: E/AndroidRuntime(29384): at
> java.lang.reflect.Method.invokeNative(Native Method) 04-03
> 21:35:07.695: E/AndroidRuntime(29384): at
> java.lang.reflect.Method.invoke(Method.java:515) 04-03 21:35:07.695:
> E/AndroidRuntime(29384): at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
> 04-03 21:35:07.695: E/AndroidRuntime(29384): at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885) 04-03
> 21:35:07.695: E/AndroidRuntime(29384): at
> dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
将以下内容更改为:
public Cursor getOpenLockers() {
Cursor c = ourDatabase.rawQuery("SELECT number, status FROM "
+ DATABASE_TABLE + " WHERE status = 'open'", null);
if (c != null) {
c.moveToFirst();
}
return c;
}
到
public Cursor getOpenLockers() {
Cursor c = ourDatabase.rawQuery("SELECT _id, number, status FROM "
+ DATABASE_TABLE + " WHERE status = 'open'", null);
if (c != null) {
c.moveToFirst();
}
return c;
}
在select中添加了_id
,同时确保DATABASE_TABLE具有_id列。
游标适配器始终需要_id
列!
希望有所帮助。!
答案 1 :(得分:0)
仔细检查是否有一个名为&#39; _id&#39;的列。在您的表中,并将查询更改为
ourDatabase.rawQuery("SELECT _id, number, status FROM "
+ DATABASE_TABLE + " WHERE status = 'open'", null);