2012-01-12 97 views
0

我是新来的android,我试图将微调值存储到数据库,但在将它存储到数据库时出错。谁能帮帮我吗。 这里是我的代码,如何将微调器值存储到数据库中?

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

我改变了代码,这样我就可以读取微调值,但是当我检查我的数据库是不是出了在微调给出确切的信息,它是显示的东西像

[email protected] 
[email protected] 

为相同的值。谁能帮帮我吗。

在此先感谢

+0

显示您logcat.Also提供你在哪里使用这些代码的细节?和你是如何管理插入,更新和删除数据库?我的意思是,你有没有创建任何类相同或不相同? – Hiral 2012-01-12 07:25:05

+0

哪里是微调?你的代码显示textview ..所以你必须得到微调或textview的价值? – 2012-01-12 07:27:46

回答

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


} 

谢谢大家....

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); 
相关问题