Reputation: 308
i am new to android, am trying to store the spinner value to the database, but am getting error while storing it into the database. can any one please help me. here is my code,
mGender = (Spinner)findViewById(R.id.spinner1);
String gender = mGender.toString();
values.put("gender", gender);
i changed the code, so i can read the spinner value, but when i check my database it is not showing the exact information that is given in the spinner, it is showing something like
android.widget.Spinner@41372738
android.widget.Spinner@41382ae0
for the same values. Can anyone please help me.
thanks in advance
Upvotes: 0
Views: 9980
Reputation: 308
finally i found the answer for this question by going through the different tutorials and samples. the solution for this is:
mGender = (Spinner)findViewById(R.id.spinner1);
// Spinner method to read the on selected value
ArrayAdapter<State> spinnerArrayAdapter = new ArrayAdapter<State>(this,
android.R.layout.simple_spinner_item, new State[] {
new State("Male"),
new State("Female")});
mGender.setAdapter(spinnerArrayAdapter);
mGender.setOnItemSelectedListener(this);
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
// Get the currently selected State object from the spinner
State st = (State)mGender.getSelectedItem();
// Show it via a toast
toastState( "onItemSelected", st );
}
public void toastState(String name, State st)
{
if ( st != null )
{
Gen = st.name;
//Toast.makeText(getBaseContext(), Gen, Toast.LENGTH_SHORT).show();
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
you have to create a spinner and assign the values in onCreate method. and one more class State for reading the spinner values.
public class State
{
public String name = "";
public State(String _name)
{
name = _name;
}
public String toString()
{
return name;
}
}
Thank You all....
Upvotes: 2
Reputation: 1641
category = (Spinner)findViewById(R.id.category_group);
category_spinner= new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line,
getResources().getStringArray(R.array.category_value));
category.setAdapter(category_spinner);
category.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
sppiner_Text= category_spinner.getItem(arg2).toString();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
//onSaveButton Click you just insert the value in DB
insert(sppiner_Text);
Upvotes: 0