私は次のコードを持っています。 onItemSelectedListenerで、選択した文字列の値をstore_idに入れて、後で処理するようにします。私はそれを調べるためにトーストを使用しました。なぜなら私はリスナーに時計を置くことができず、store_idはデフォルトのヌルです。スピナーは私のいろいろな文字列を表示しますが、クリックすると消えます。何か案は?あなたがする必要はありませんので、私は、あなたが既にspinner
のリスナーを登録している...私の問題は、一緒にこのフラグメントのすべてのコードを持っているoutput right before clicking on a storeこのフラグメントのonItemSelectedListenerからスピナーのクリックされたアイテムを取得するにはどうすればよいですか?
public class ChooseStoreFragment extends Fragment implements AdapterView.OnItemSelectedListener {
ArrayList<String> store_array = new ArrayList<>();
String store_id;
//implementing the AdapterView.OnItemSelectedListener
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String user_store_id = parent.getItemAtPosition(position).toString();//doesn't show up in toast
store_id = parent.getSelectedItem().toString();//doesn't show up in toast
Toast.makeText(getActivity(), user_store_id + "=user_store_id.", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(parent.getContext(),"Nothing selected",Toast.LENGTH_SHORT).show();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View thisView = inflater.inflate(fragment_choose_store, container, false);
final Spinner spinner = (Spinner) thisView.findViewById(R.id.stores_spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this.getContext(),android.R.layout.simple_spinner_dropdown_item,store_array);
spinner.setAdapter(adapter);
AdapterView.OnItemSelectedListener mListener = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
store_id = parent.getSelectedItem().toString();
} //still never shows up in toast
@Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getActivity(), "none found = user_store_id.", Toast.LENGTH_SHORT).show(); //still nothing
}
};
spinner.setOnItemSelectedListener(mListener);
。あなたはどれが欲しいですか? – BakaWaii
最初のもの。 2番目の実装は、より多くの地域が問題になったという試みでした! –