gpt4 book ai didi

java - Android:无法从原始查询填充 ListView

转载 作者:行者123 更新时间:2023-11-30 02:58:56 26 4
gpt4 key购买 nike

我正在尝试从原始查询语句更新 ListView ,但是我遇到了一些错误。

我的 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)

最佳答案

将以下内容更改为:

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;
}

在选择中添加了 _id,同时确保 DATABASE_TABLE 具有列 _id。

游标适配器总是需要一个 _id 列!

希望对您有所帮助!

关于java - Android:无法从原始查询填充 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22853721/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com