吐司显示两次

时间:2015-11-21 15:21:17

标签: android if-statement spinner 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小时'吐司。我无法弄清楚代码有什么问题。请帮帮忙?!

3 个答案:

答案 0 :(得分:3)

在onCreateView中设置onItemSelectedListener时可能会调用它,或者在最初显示视图时可能会调用它。一个hacky解决方法就是拥有一个布尔值,只有在之前至少调用过一次之后才会执行监听器中的代码。

答案 1 :(得分:2)

IIRC,旋转器必须有一个选定的项目,无论如何,所以它在启动时设置第一个项目,因此当微调器启动时会触发OnItemSelected。除非你想破解微调器代码,否则无法绕过它。

你可以做的是添加一个计数器变量,以确定它是否是第一次通过,如果是第一次不显示吐司。

答案 2 :(得分:1)

这是因为当您将适配器设置为微调器时,您的adpater onItemSelected会被调用。在if块中添加调试点。解决方案是使用布尔标志来检查用户是否选择了微调器。最初是假的。然后在您的开关检查该标志是否为假之前。如果它是假的,那么不要执行切换,否则执行此操作。在switch case之后使boolean标志为true。因此,对于第一次加载,它将是假的,并且在加载片段时不会出现toast。