如何将微调器值存储到数据库中?

时间:2012-01-12 07:10:31

标签: android sqlite android-spinner

我是android的新手,我试图将微调器值存储到数据库中,但是在将其存储到数据库时会出错。谁能帮帮我吗。 这是我的代码,

mGender = (Spinner)findViewById(R.id.spinner1);
String gender = mGender.toString();
values.put("gender", gender);

我更改了代码,所以我可以读取微调器值,但是当我检查我的数据库时它没有显示微调器中给出的确切信息,它显示的内容类似于

android.widget.Spinner@41372738
android.widget.Spinner@41382ae0

表示相同的值。任何人都可以帮助我。

提前致谢

2 个答案:

答案 0 :(得分:1)

最后,我通过不同的教程和示例找到了这个问题的答案。对此的解决方案是:

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

}

您必须创建一个微调器并在onCreate方法中指定值。还有一个用于读取微调器值的类状态。

public class State 
{
    public String name = "";


    public State(String _name)
    {

        name = _name;

    }
    public String toString()
    {
        return name;
    }


}

谢谢大家......

答案 1 :(得分:0)

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);