Can't save spinner selected item to database, it saves android.sqlite.cursor@ not the string

StackOverflow https://stackoverflow.com/questions/21986084

  •  15-10-2022
  •  | 
  •  

Question

My problem is the sp1.getSelectedItem().toString(); only saves the data as android.database.SQLite.cursor@blablabla ... it's not getting the string as I declared... How can I do this?

here's my code for cursor[populating the spinner]

 cursor = mySQLiteAdapter.queueSchoolAll();
    cursor.requery();
    String[] from1 = new String[]{Database.KSCHOOLCODE};
    int[] to1 = new int[]{R.id.rt1};       
    cursorAdapter =
        new SimpleCursorAdapter(this, R.layout.a_row_school, cursor, from1, to1);
    sp1.setAdapter(cursorAdapter);

here's the code of the button that will save the data:

 String data1 = (String)sp1.getSelectedItem().toString();
 mySQLiteAdapter.insertGrade(data1);
Was it helpful?

Solution

Try like

  String data1 = ((Cursor) sp1.getSelectedItem()).getString(0);
  mySQLiteAdapter.insertGrade(data1);

OTHER TIPS

You need to get data from cursor. For example:

public xxxxxx onItemClickFunction(..., int position,....){
int pos = cursor.getPosition();//saving the position just in case

cursor.moveToPostion(position);// moving to the position where a click is fired
String val = cursor.getString(column index);

cursor.moveToPosition(pos);// moving back to original position.

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top