Return which Alert Dialog button was clicked

0

I have a void method that creates a custom dialog that shows a listview multichoice, I would like this method to return a boolean when the positivebutton or negativebutton is clicked to do another operation on the method that calls it.

Void method

    public void listaRecorrente(final List<Receita> list) {
    AlertDialog.Builder ab = new AlertDialog.Builder(this);
    ab.setTitle("Parcelas");
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.list_dialog, null);
    final ListView lv = (ListView) view.findViewById(R.id.list_dlg);
    final Button tudo, nada;
    tudo = (Button) view.findViewById(R.id.btTodas);
    nada = (Button) view.findViewById(R.id.btNenhuma);
    tudo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            for (int o = 0; o < lv.getAdapter().getCount(); o++)
                lv.setItemChecked(o, true);
        }
    });
    nada.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            for (int o = 0; o < lv.getAdapter().getCount(); o++)
                lv.setItemChecked(o, false);
        }
    });
    lv.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
    lv.setDivider(null);
    final ReceitaDAO dao = new ReceitaDAO(this);
    ArrayAdapter<Receita> aa = new ArrayAdapter<>(this, android.R.layout.select_dialog_multichoice, list);
    lv.setAdapter(aa);
    ab.setView(view);
    ab.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();
            final Handler mHandler = new Handler();
            progress.show();
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    int len = lv.getCount();
                    SparseBooleanArray checked = lv.getCheckedItemPositions();
                    for (int y = 0; y < len; y++) {
                        if (checked.get(y)) {
                            dao.deletarRecorrente(list.get(y).getId());
                            dao.deletar(list.get(y));
                        }
                    }
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            progress.hide();
                        }
                    });
                }
            });
            t.start();
            chamaToast("Receitas excluídas!");
        }
    });
    ab.setNegativeButton("CANCELAR", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();
        }
    });
    ab.show();        
}
    
asked by anonymous 15.01.2016 / 14:01

2 answers

0

Following the reasoning line of Renan Diniz's comment, I passed what I wanted to execute (finalize the action mode) after the method returns true or false as a parameter.

It looks like this:

public void listaRecorrente(final List<Receita> list, final ActionMode actionMode) {
    AlertDialog.Builder ab = new AlertDialog.Builder(this);
    ab.setTitle("Parcelas");
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.list_dialog, null);
    final ListView lv = (ListView) view.findViewById(R.id.list_dlg);
    final Button tudo, nada;
    tudo = (Button) view.findViewById(R.id.btTodas);
    nada = (Button) view.findViewById(R.id.btNenhuma);
    tudo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            for (int o = 0; o < lv.getAdapter().getCount(); o++)
                lv.setItemChecked(o, true);
        }
    });
    nada.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            for (int o = 0; o < lv.getAdapter().getCount(); o++)
                lv.setItemChecked(o, false);
        }
    });
    lv.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
    lv.setDivider(null);
    final ReceitaDAO dao = new ReceitaDAO(this);
    ArrayAdapter<Receita> aa = new ArrayAdapter<>(this, android.R.layout.select_dialog_multichoice, list);
    lv.setAdapter(aa);
    ab.setView(view);
    ab.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();
            final Handler mHandler = new Handler();
            progress.show();
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    int len = lv.getCount();
                    SparseBooleanArray checked = lv.getCheckedItemPositions();
                    for (int y = 0; y < len; y++) {
                        if (checked.get(y)) {
                            dao.deletarRecorrente(list.get(y).getId());
                            dao.deletar(list.get(y));
                        }
                    }
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            progress.hide();
                        }
                    });
                }
            });
            t.start();
            actionMode.finish();
            refreshLista();
            chamaToast("Receitas excluídas!");

        }
    });
    ab.setNegativeButton("CANCELAR", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();
        }
    });
    ab.show();
}
    
18.01.2016 / 13:36
1

There is no way to do this in this method, it will execute on the dialog creation and not when the buttons are clicked. You should use the methods that listen for the click event, onClick, to perform this operation.

    
15.01.2016 / 15:16