我有以下代碼。我希望onItemSelectedListener將所選字符串的值放入store_id以供進一步處理。我使用Toast來檢查它,因爲我無法將監視放在監聽器上,而store_id是默認爲空。微調員顯示我的各種字符串,但點擊時消失。有任何想法嗎?我不能想象我的問題是具有在該片段在一起的所有代碼... 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);
你已經實現了兩個'AdapterView.OnitemSelectedListener'。你想要哪一個? – BakaWaii
第一個。第二個實現只是試圖讓更多的地方成爲一個問題!? –