Sakir Alam Sakir Alam - 2 months ago 28
Android Question

How to store value of spinner button in sqlite database

As i am new in Android development I dnt know how to store selected value of a spinner in sqlite database. Here is the java code. Also inside onClick savebutton the following line is an error: String age = sp.getText().toString()

public class SqlLiteExample extends Activity implements OnClickListener, OnItemSelectedListener{
Button sqlUpdate, sqlView;
EditText etName,etContact;
RadioGroup myRadioGrp;
RadioButton rbtn1,rbtn2;
Spinner sp;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlliteexample);
sqlUpdate = (Button) findViewById(R.id.savebutton);
etName = (EditText) findViewById(R.id.editName);
etContact = (EditText) findViewById(R.id.editContact);
rbtn1 = (RadioButton) findViewById(R.id.malebutton);
rbtn2 = (RadioButton) findViewById(R.id.femalebutton);
sqlView = (Button) findViewById(R.id.viewbutton);
sqlView.setOnClickListener(this);
sqlUpdate.setOnClickListener(this);

uI();
spElements();

}

private void spElements() {
// TODO Auto-generated method stub
List<String> ages = new ArrayList<String>();
for(int i = 18; i <= 99; i++) {
ages.add(String.valueOf(i));
}
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, ages);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
sp.setAdapter(dataAdapter);
}

private void uI() {
// TODO Auto-generated method stub
sp = (Spinner) findViewById(R.id.spAge);
sp.setOnItemSelectedListener(this);
}

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.savebutton:
boolean didWork = true;
try{
String name = etName.getText().toString();
String age = sp.getText().toString();
String contact = etContact.getText().toString();

myRadioGrp =(RadioGroup)findViewById(R.id.RadioGroup01);
String sex = ((RadioButton)this.findViewById(myRadioGrp.getCheckedRadioButtonId())).getText().toString();

MyDB entry = new MyDB(SqlLiteExample.this);
entry.open();
entry.createEntry(name,age,contact,sex);
entry.close();
}catch(Exception e){
didWork = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("Error");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
}finally{
if(didWork){
Dialog d = new Dialog(this);
d.setTitle("Updated");
TextView tv = new TextView(this);
tv.setText("Success");
d.setContentView(tv);
d.show();
}
}
break;
case R.id.viewbutton:
Intent i = new Intent("com.bysakiralam.mydatabase.DISPLAYRECORDS");
startActivity(i);
break;
}
}

@Override
public void onItemSelected(AdapterView<?> main, View arg1, int position, long Id) {
// TODO Auto-generated method stub
String item = main.getItemAtPosition(position).toString();


}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}


}

Answer

Try using

String age = (String) sp.getSelectedItem();

As you are using an ArrayAdapter, your selected item is a String, and you can cast it.

Comments