我想使用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)
如何解决此错误?
答案 0 :(得分:1)
只使用一个光标来获取两列
String sql = "SELECT ip,description FROM " + ServerEntry.TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
while(cursor.moveToNext()) {
String multiRow = cursor.getString(cursor.getColumnIndex("ip")) + " - " + cursor.getString(cursor.getColumnIndex("description"));
labels.add(multiRow);
}
答案 1 :(得分:1)
您在第一条记录中仅移动了一个 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 。
因此,例外。
Index -1 requested, with a size of 2
我可以问你为什么在地球上你正在执行 2个不同的查询,而你可以获得所有必需的字段单个 ?
即:
String oneSingleQuery = "SELECT ip, description FROM " + ServerEntry.TABLE_NAME;
然后你只需处理单光标。