如何在sqlite数据库中存储微调按钮的值

时间:2013-07-11 21:17:51

标签: android android-layout android-listview sqlite

由于我是Android开发的新手,我不知道如何在sqlite数据库中存储微调器的选定值。这是java代码。同样在onClick savebutton中,以下行是一个错误: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

}

}

2 个答案:

答案 0 :(得分:0)

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String selected = parent.getItemAtPosition(position).toString();
            //Code to insert selected string in database
        }
    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
});

答案 1 :(得分:-1)

尝试使用

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

当您使用ArrayAdapter时,您选择的项目是一个字符串,您可以投射它。