如何存储在SQLite数据库微调按钮的值 [英] How to store value of spinner button in sqlite database

查看:168
本文介绍了如何存储在SQLite数据库微调按钮的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我在Android开发新的二硝基甲苯我知道如何存储在SQLite数据库纺丝器选择的值。这里是java code。另外里面的onClick savebutton下面一行是错误的:字符串年龄= sp.getText()的toString()

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

}

}

推荐答案

尝试使用

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

由于您使用的是ArrayAdapter,您选择的项目是一个字符串,你可以投它。

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

这篇关于如何存储在SQLite数据库微调按钮的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆