0

我有以下代碼。我希望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); 
+0

你已經實現了兩個'AdapterView.OnitemSelectedListener'。你想要哪一個? – BakaWaii

+0

第一個。第二個實現只是試圖讓更多的地方成爲一個問題!? –

回答

0

您已經註冊了一個偵聽器spinner,所以你不必做ChooseStoreFragment執行該listener再次。

spinner.setOnItemSelectedListener(mListener); 

它應該是這樣的

public class ChooseStoreFragment extends Fragment { 
    ArrayList<String> store_array = new ArrayList<>(); 
    String store_id; 

    @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); // Register this spinner for a mListener 
    } 
} 

或者,如果你想爲你的類

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); 
     spinner.setOnItemSelectedListener(this);// register this fragment with a listener 
    } 
} 

註冊登記監聽器只有1聽衆!

更新:

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 = store_array.get(position); 
     Toast.makeText(getActivity(), user_store_id, Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onNothingSelected(AdapterView<?> parent) { 
     Toast.makeText(getActivity(), "Nothing selected", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     final View thisView = inflater.inflate(R.layout.fragment_choose_store, container, false); 
     final Spinner spinner = (Spinner) thisView.findViewById(R.id.stores_spinner); 
     ArrayAdapter<String> adapter = new ArrayAdapter<>(this.getActivity(), android.R.layout.simple_spinner_dropdown_item, store_array); 
     spinner.setAdapter(adapter); 
     spinner.setOnItemSelectedListener(this);// register this fragment with a listener 
     return thisView; 
    } 
} 
+0

我喜歡你的代碼,謝謝你,乾淨利落,但仍然沒有數據從onItemSelected到store_id。你可以看看parent.getSelectedItem()行/方法嗎? –

+0

因爲你store_array不包含任何 –

+0

但它時,它會通過一個VolleyRequest裝載和微調顯示的數據。我的輸出中的截圖在我的問題中尚未被允許(低排名)。你會看看截圖嗎? –

相關問題