我在一個片段中有兩個旋鈕,其中一個旋鈕會在選擇某個項目時顯示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小時'的烤麪包。我無法弄清楚代碼有什麼問題。請幫助?!
http://stackoverflow.com/questions/5624825/spinner-onitemselected-executes-when-it-is-not-suppose-to/5918177#5918177 –
感謝您的鏈接。我嘗試了鏈接的解決方案,它的工作原理!謝謝。 –