1

スピナーで選択した値を取得し、必要に応じてデータを読み取って sqlite データベースに格納する文字列として返したいと考えています。

私は以下の方法を試して、

sp = (Spinner) findViewById(R.id.spnCategory);      
ArrayAdapter<CharSequence> ar = ArrayAdapter.createFromResource(this,
                R.array.category, android.R.layout.simple_list_item_1);
ar.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
sp.setAdapter(ar);
String selection = sp.getSelectedItem().toString();

以下のように文字列エクストラを取得します。

selection = extras.getString("category");

以下のようにエクストラを入れます。

v.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent=new Intent(context,ViewItem.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);               

                intent.putExtra("category", category);

            }
        });

しかし、選択したスピナーの値を取得できません。スピナーで最初の値を取得しただけです。コーディングの何が問題なのか分かりますか?

4

1 に答える 1

2

使用するonItemSelected

 public void onItemSelected(AdapterView<?> parent, View view, 
        int pos, long id) {
    // An item was selected. You can retrieve the selected item using
    // parent.getItemAtPosition(pos)

    TextView tv = (TextView)view;
    String selection = tv.getText().toString();   // or you can use the position but I do this to do other things with the TextView 
}

public void onNothingSelected(AdapterView<?> parent) {
    // Another interface callback
}
}

Spinner 値の取得

そして、あなたがあなたにいることを確認implements OnItemSelectedListenerし、あなたActivityにリスナーを設定しますSpinner

sp.setOnItemSelectedSpinner(this);

ノート

selection他の場所で使用してここに割り当てることができるように、メンバー変数として宣言することもできます

于 2013-06-01T16:17:32.140 に答える