2015-11-21 137 views
0

我在一個片段中有兩個旋鈕,其中一個旋鈕會在選擇某個項目時顯示Toast消息。問題是第一個if-else語句中的敬酒顯示兩次。一旦打開片段,第二次選擇該項目時。Toast顯示兩次

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    mRootView = inflater.inflate(R.layout.add, container, false); 



    //------------------------------unit Spinner adapter---------------------------------------// 
    Spinner spinner = (Spinner) mRootView.findViewById(R.id.units); 

    //Create ArrayAdapter using string array and default spinner 
    ArrayAdapter<CharSequence> sAdapter = ArrayAdapter.createFromResource(getActivity(), R.array.units, android.R.layout.simple_spinner_dropdown_item); 

    //Specify layout to use when list of choices appears 
    sAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

    //Apply adapter to the spinner 
    spinner.setAdapter(sAdapter); 


    //----------------------------reminder Spinner adapter-------------------------------------// 
    Spinner reminderSpinner = (Spinner) mRootView.findViewById(R.id.list_reminder); 
    ArrayAdapter<CharSequence> reminderAdapter = ArrayAdapter.createFromResource(getActivity(), R.array.countdown_reminder, android.R.layout.simple_spinner_dropdown_item); 
    reminderAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    reminderSpinner.setAdapter(reminderAdapter); 



    spinner.setOnItemSelectedListener(spinnerListener); 
    reminderSpinner.setOnItemSelectedListener(spinnerListener); 

return mRootView; 
} 


    //---------------------------------Spinner Listener----------------------------------------// 
    AdapterView.OnItemSelectedListener spinnerListener = new AdapterView.OnItemSelectedListener() { 

     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 

      switch (parent.getId()) { 

       case R.id.units: 
        String selectedUnit = parent.getItemAtPosition(position).toString(); 
        break; 
       case R.id.list_reminder: 
        String reminder = parent.getItemAtPosition(position).toString(); 


        if (reminder.equals("24 hours")) { 
         Toast.makeText(getActivity(), "Reminder has been set 24 hours from the selected date", Toast.LENGTH_LONG).show(); 

        } else if (reminder.equals("2 days")) { 
         Toast.makeText(getActivity(), "Reminder has been set 2 days from the selected date", Toast.LENGTH_LONG).show(); 

        } else if (reminder.equals("3 days")) { 
         Toast.makeText(getActivity(), "Reminder has been set 3 days from the selected date", Toast.LENGTH_LONG).show(); 

        } else if (reminder.equals("1 week")) { 
         Toast.makeText(getActivity(), "Reminder has been set 1 week from the selected date", Toast.LENGTH_LONG).show(); 

        } else if (reminder.equals("2 weeks")) { 
         Toast.makeText(getActivity(), "Reminder has been set 2 weeks from the selected date", Toast.LENGTH_LONG).show(); 

        } else if (reminder.equals("1 month")) { 
         Toast.makeText(getActivity(), "Reminder has been set 1 month from the selected date", Toast.LENGTH_LONG).show(); 
        } 
        break; 
      } 
     } 


     public void onNothingSelected(AdapterView<?> parent) { 
     } 
    }; 
    //------------------------------------end spinner code-------------------------------------// 

當用戶打開片段和選擇'24小時'時顯示'24小時'的烤麪包。我無法弄清楚代碼有什麼問題。請幫助?!

+0

http://stackoverflow.com/questions/5624825/spinner-onitemselected-executes-when-it-is-not-suppose-to/5918177#5918177 –

+0

感謝您的鏈接。我嘗試了鏈接的解決方案,它的工作原理!謝謝。 –

回答

3

當您在onCreateView中設置它時,您的onItemSelectedListener可能會被調用,或者它在最初顯示視圖時可能會被調用。一個怪異的解決方法是隻有一個布爾值,它至少在調用過一次之後才執行監聽器中的代碼。

2

IIRC,無論如何,微調控制器都必須有選定的項目,所以它在啓動時設置第一個項目,因此在微調器啓動時會觸發OnItemSelected。沒有辦法解決它,除非你想破解微調代碼。

你可以做的是添加一個計數器變量,以確定它是否是第一次通過,如果是第一次不顯示吐司。

1

這是因爲在將適配器設置爲微調器時,將會調用您的adpater的onItemSelected。在if塊中添加一個調試點。解決方案是使用布爾標誌來檢查用戶是否選擇了微調器。最初使其爲假。那麼在切換之前檢查該標誌是否爲假。如果它是假的,那麼不要執行切換,否則做。在切換大小寫後使布爾標誌爲真。因此,對於第一次加載它將是錯誤的,並且在加載片段時不會出現烤麪包。