2012-01-12 25 views
0

私はアンドロイドには新しく、スピナー値をデータベースに保存しようとしていますが、データベースに保存中にエラーが発生しています。誰でも私を助けてください。ここ がスピナーの値をデータベースに格納する方法は?

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

、私のコードである私は、コードを変更するので、私はスピナー値を読み取ることができますが、私はそれがスピナーに与えられている正確な情報を示していない私のデータベースを確認したときに、それが何かを示していますlike

[email protected] 
[email protected] 

と同じ値です。誰でも助けてくれますか?事前に

おかげ

+0

....あなたのすべてに感謝し、あなたlogcat.Alsoは、あなたがこれらのコード行を使用していますか?そしてどのように挿入を管理している場所の詳細を提供する、更新およびデータベース上で削除表示しますか?私はあなたが同じかどうかのための任意のクラスを作成したことを意味ですか? – Hiral

+0

どこにスピナーがありますか?あなたのコードはtextviewを示している..あなたはスピナーやtextviewの価値を取得する必要がありますか? –

答えて

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メソッドの値を割り当てる必要があります。スピナー値を読み取るためのもう1つのクラスの状態。

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); 
関連する問題