AlertDialog在onOptionsItemSelected中不能很好地工作

时间:2018-04-10 23:58:31

标签: java android android-fragments

我在片段中的onOptionsItemSelected中有一个AlertDialog,第一次运行良好,但第二次打开时我有两个选项 1.当onOptionsItemselected返回true时 显示以前的AlertDialog,并且AlertDialog不能正常工作 2.当onOptionsItemselected返回false时 显示两个或更多AlertDialog,但第一个AlertDialog工作,但另一个出现,他们不做anythyng

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
        switch(item.getItemId()){
            case R.id.action_sortP:

                final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                LayoutInflater inflater = this.getLayoutInflater();
                View dialogView = inflater.inflate(R.layout.dialogue, null);
                builder.setView(dialogView);
                final AlertDialog dialog = builder.create();
                final RadioGroup radioGroupBy = dialogView.findViewById(R.id.radio_groudBy);
                final RadioGroup radioGroupOr = dialogView.findViewById(R.id.radio_groupOr);

                if (radioButtonIDby != 0){
                    RadioButton radioButtonBy1 = radioGroupBy.findViewById(radioButtonIDby);
                    radioButtonBy1.setChecked(true);
                }
                if (radioButtonIDor != 0){
                    RadioButton radioButtonOr1 = radioGroupOr.findViewById(radioButtonIDor);
                    radioButtonOr1.setChecked(true);
                }

                Button buttonCancel = dialogView.findViewById(R.id.cancel);
                buttonCancel.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                    }
                });

                Button button = dialogView.findViewById(R.id.btn_ok);
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        radioButtonIDby = radioGroupBy.getCheckedRadioButtonId();
                        radioButtonIDor = radioGroupOr.getCheckedRadioButtonId();

                        if (radioButtonIDby == R.id.radio_nombre && radioButtonIDor == R.id.radio_asc){
                            Collections.sort(listLugares, new Comparator<LugarTuristico>() {
                                @Override
                                public int compare(LugarTuristico o1, LugarTuristico o2) {
                                    return o1.getName_lugar().compareTo(o2.getName_lugar());
                                }
                            });
                        }else if(radioButtonIDby == R.id.radio_nombre && radioButtonIDor == R.id.radio_desc){
                            Collections.sort(listLugares, new Comparator<LugarTuristico>() {
                                @Override
                                public int compare(LugarTuristico o1, LugarTuristico o2) {
                                    return o2.getName_lugar().compareTo(o1.getName_lugar());
                                }
                            });
                        }else if(radioButtonIDby == R.id.radio_tipo && radioButtonIDor == R.id.radio_asc){
                            Collections.sort(listLugares, new Comparator<LugarTuristico>() {
                                @Override
                                public int compare(LugarTuristico o1, LugarTuristico o2) {
                                    return o1.getTipo_lugar().compareTo(o2.getTipo_lugar());
                                }
                            });
                        }else if(radioButtonIDby == R.id.radio_tipo && radioButtonIDor == R.id.radio_desc){
                            Collections.sort(listLugares, new Comparator<LugarTuristico>() {
                                @Override
                                public int compare(LugarTuristico o1, LugarTuristico o2) {

                                    return o2.getTipo_lugar().compareTo(o1.getTipo_lugar());
                                }
                            });
                        }else if(radioButtonIDby == R.id.radio_valoracion && radioButtonIDor == R.id.radio_asc){
                            Collections.sort(listLugares, new Comparator<LugarTuristico>() {
                                @Override
                                public int compare(LugarTuristico o1, LugarTuristico o2) {
                                    return new Double(o2.getValoracion()).compareTo(new Double(o1.getValoracion()));
                                }
                            });
                        }else if(radioButtonIDby == R.id.radio_valoracion && radioButtonIDor == R.id.radio_desc){
                            Collections.sort(listLugares, new Comparator<LugarTuristico>() {
                                @Override
                                public int compare(LugarTuristico o1, LugarTuristico o2) {
                                    return new Double(o1.getValoracion()).compareTo(new Double(o2.getValoracion()));
                                }
                            });
                        }
                        dialog.dismiss();
                        setAdaptadorRVLista(listLugares);
                    }
                });
                dialog.show();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

第一次更新,但我第二次打开,不工作或累积AlertDialog

1 个答案:

答案 0 :(得分:0)

尝试使用dialog.cancel();而不是解雇