I was successful in getting a value from the spinner and storing it in a databse, but at return, I couldn’t set Text into Spinner. How do I do this like the other function of EditText?
This is my code:
public class MainActivity_spinner extends Activity
{
Button save;
Button show;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] items = new String[] {" ","Male","Female"};
final Spinner gender =(Spinner)findViewById(R.id.sex);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
gender.setAdapter(adapter);
save=(Button)findViewById(R.id.save);
show=(Button)findViewById(R.id.show);
//----------------------------------------------------------------
save.setOnClickListener(new Button.OnClickListener() {
public void onClick(View view){
ContentValues values = new ContentValues();
values.put("sex",gender.getSelectedItem().toString());
sql.Insert("db",null,values);
sql.Close();
}
});
show.setOnClickListener(new Button.OnClickListener() {
public void onClick(View view){
sql.open();
String query = "select sex from db where id=2";
Cursor c = sql.rawQuery(query,null);
c.moveToFirst();
(Error is here)it is my spinner i want show value >>>>>> gender.setSelection(c.getString(0), true);
}
});
}
}
I saved the second item into my database from my spinner. Now my spinner is reset. Then, I click on the Show button. The item I saved will show in the spinner as data. Why is this failing?
If i didn’t get you wrong,you are trying to find the way you can set a text as selected item in spinner,which is one of the items from your database you have list of,in spinner.
If so,then this might help you:
Example:
This will be like:
You have these options in database:
1.apple
2.orange
3.pineapple
4.strawberry
5.grapes
now,you want to show pineapple to be selected,then,
this line will set it accordinly.
EDIT :
Try with this: