2012-01-12 77 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); 
相關問題