0

I am trying to get a string from a spinner which gets its information from a database. When using

spinner.getSelectedItem().toString();

I get strings like "android.database.sqlite.SQLiteCursor@412f3ff8". To get the resolved string, I tried to query the database using a cursor but it causes an IllegalStatException while a message in LogCat appears saying "Failed to read row 1, column 2 from a CursorWindow which has 3 rows, 2 columns.".

The numbers for the row and column are correct as the cell which should be found there contains the value I want to query.

Here's the code I used:

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)

Thank you, for your help.

NiPfi
  • 1,710
  • 3
  • 18
  • 28
  • i can suggest you one thing. If u want the value in the column 2 you need to query selectioncursor.getString(1); and not selectioncursor.getString(2); because cursor index starts from 0.can you post the logcat please – G_S Sep 05 '12 at 15:56
  • @SharathG That did already solve my problem. Thank you :) – NiPfi Sep 05 '12 at 16:15
  • oh that's nice to hear that its already solved by yourself. cheers – G_S Sep 05 '12 at 16:20

1 Answers1

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);
ketan
  • 19,129
  • 42
  • 60
  • 98