gpt4 book ai didi

java - CursorIndexOutOfBoundsException?

转载 作者:行者123 更新时间:2023-12-02 02:37:16 27 4
gpt4 key购买 nike

我想使用 List 和 Cursor 将两个值放入 SQLite 数据库中的一个微调器行中,这是我的代码:

将数据放入列表:

public List<String> getAllLabelsServer(){
List<String> labels = new ArrayList<String>();



// Select All Query
String selectQuery = "SELECT ip FROM " + ServerEntry.TABLE_NAME;
String description = "SELECT description FROM " + ServerEntry.TABLE_NAME;

SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
Cursor cursor1 = db.rawQuery(description, null);



// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
String multiRow = cursor.getString(0) + " - " + cursor1.getString(0);
labels.add(multiRow);
} while (cursor.moveToNext());
}

// closing connection
cursor.close();
db.close();

// returning lables
return labels;
}

将数据从列表添加到微调器:

private void loadSpinnerDataCommand() {
// database handler
DbHelper db = new DbHelper(getApplicationContext());

// Spinner Drop down elements
List<String> labels = db.getAllLabelsCommand();

// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, labels);

// Drop down layout style - list view with radio button
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

// attaching data adapter to spinner
spinnerCommand.setAdapter(dataAdapter);
}

但是当我尝试获取 cursor1.getString(0) 时出现错误:

Process: com.example.admin.ssh, PID: 14389 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.admin.ssh/com.example.admin.ssh.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 2 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2684) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2751) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1496) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6186) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779) Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 2 at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460) at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50) at com.example.admin.ssh.data.DbHelper.getAllLabelsServer(DbHelper.java:108) at com.example.admin.ssh.MainActivity.loadSpinnerDataServer(MainActivity.java:222) at com.example.admin.ssh.MainActivity.onCreate(MainActivity.java:86) at android.app.Activity.performCreate(Activity.java:6684) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2637) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2751) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1496) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6186) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

如何解决这个错误?

最佳答案

您仅在第一条记录上移动了 2 个光标(名为 cursor 的光标)。

    // looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
String multiRow = cursor.getString(0) + " - " + cursor1.getString(0);
labels.add(multiRow);
} while (cursor.moveToNext());
}

因此,第二个(名为cursor1)仍然指向索引-1

因此,异常(exception)。

Index -1 requested, with a size of 2

请问您到底为什么要执行2个不同的查询,而您可以使用单个一个查询来获取所有必填字段?

即:

String oneSingleQuery = "SELECT ip, description FROM " + ServerEntry.TABLE_NAME;

然后您也只需处理单个光标

关于java - CursorIndexOutOfBoundsException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46112360/

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