Return which Alert Dialog button was clicked

Asked

Viewed 429 times

0

I have a void method that creates a custom dialog that shows a listview Multichoice, I would like to make this method return a Boolean when positivebutton or negativebutton is clicked to do another operation in 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();        
}
  • 1

    I couldn’t figure out which method you want to return the Boolean? The operations you must do after a positivebutton or negativeButton button is pressed must be implemented within the onClick button.

2 answers

1

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

0


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

Stayed 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();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.