0

我试图从一个从数据库中获取信息的微调器中获取一个字符串。使用时

spinner.getSelectedItem().toString();

我得到像“android.database.sqlite.SQLiteCursor@412f3ff8”这样的字符串。为了获得已解析的字符串,我尝试使用游标查询数据库,但它会导致 IllegalStatException,同时 LogCat 中出现一条消息说“无法从具有 3 行 2 列的 CursorWindow 读取第 1 行第 2 列。”。

行和列的数字是正确的,因为应该在那里找到的单元格包含我要查询的值。

这是我使用的代码:

int selection = (int) spinner.getSelectedItemId(); //I get the selected item id.
        Cursor selectioncursor = db.getAllSubjects(); //This is a query getting all the contents from the table
        selectioncursor.moveToPosition(selection); //The id I got earlier equals the number of the row, the data is stored in, so I move the cursor to the correct row.
        String subject = selectioncursor.getString(2); //Now the cursor should get the string from column 2 which is the one containing all the values (first column is "_id" of course)

谢谢您的帮助。

4

1 回答 1

0
 static void llenaMiSpinner(Cursor cursor, String dato, Spinner spinner, Context context) {
        cursorsimpleAdapter = new SimpleCursorAdapter(context,
                android.R.layout.simple_spinner_item,
                cursor,
                new String[]{dato},
                new int[]{android.R.id.text1},
                SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER
        );
        spinner.setAdapter(cursorsimpleAdapter);
    }

// aqui puedes obtener tu informacion :) 
  @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
        int vista = adapterView.getId();
        switch (vista) {
            case R.id.spinner:
                c3 = (Cursor) adapterView.getItemAtPosition(position);
                objetoid = c3.getString(c3.getColumnIndex(recursoBD.Objeto.OBJ_ID));
                break;
            case R.id.spinner2:
                Cursor c4 = (Cursor) adapterView.getItemAtPosition(position);
                marcaId = c4.getString(c4.getColumnIndex(recursoBD.Marca.MAR_ID));
                break;

            case R.id.spinner3:
                //obteiendo ID del Eje que fue Selecionado
                Cursor c1 = (Cursor) adapterView.getItemAtPosition(position);
                ejeSelection = c1.getString(
                        c1.getColumnIndex(recursoBD.Eje.EJE_ID));
                activeTramoSpinner(ejeSelection);

                break;
            case R.id.spinner4:
                Cursor c2 = (Cursor) adapterView.getItemAtPosition(position);
                tramoSelection = c2.getString(c2.getColumnIndex(recursoBD.Tramo.TRAMO_ID));

                break;
            case R.id.spinner5:
                Cursor c5 = (Cursor) adapterView.getItemAtPosition(position);
                id_usuario = c5.getString(c5.getColumnIndex(recursoBD.Usuario.USU_ID));
                break;
        }
    }


llenaMiSpinner(cursorDispo, DISPO_NAME, objetos, this);
于 2015-08-17T04:06:50.710 回答