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
  •  | 
  •  

문제

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);
도움이 되었습니까?

해결책

Try like

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

다른 팁

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.

}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top