我想使用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)


我该如何解决这个错误?

最佳答案

在第一条记录上,您仅移动了两个光标中的一个(名为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


请问为什么在地球上要执行两个不同的查询,而一个查询却可以得到所有必填字段?

即:

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


然后,您也只需要处理一个光标。

10-08 00:18