How to update the content of a Dialog

950 Views Asked by At

I am trying to update the content of a dialog after the user chooses a Quantity from a List dialog. For example now the user is presented with my "add Item dialog": enter image description here

and the default quantity is 1, but if the user decides that he/she wants more presses on Add Quantity (neutral) button and it is presented with another dialog: enter image description here The problem is, after the user chooses a quantity my code does not refresh the content of the first dialog as I want, it still says 1. BTW I am using a 3th party library for the creation of the Dialogs This is my code:

  final int[] choosenQuantity = {1};
    final String[] str = {""};
    final MaterialDialog.Builder addItemBuilder = new MaterialDialog.Builder(this)
    .title("Add Item")
    .widgetColor(getResources().getColor(R.color.ColorPrimaryDark))
    .inputMaxLength(30, R.color.material_blue_grey_950)
            .content("Quantity: "+choosenQuantity[0]+"")
    .inputType(InputType.TYPE_CLASS_TEXT)
            .autoDismiss(false)
            .input("add shopping item", "", new MaterialDialog.InputCallback() {
                @Override
                public void onInput(MaterialDialog dialog, CharSequence input) {
                    str[0] = input.toString();
                    //add it to shoppingListItems and save to sharedPreferences
                    if (str[0].length() != 0) {
                        shoppingListItems.add(str[0] + " (" + choosenQuantity[0] + ")");
                        saveShoppingItems();
                        isListEmpty();
                        dialog.dismiss();
                    } else {
                        Toast.makeText(MainActivity.this, "no item description!", Toast.LENGTH_LONG).show();
                    }

                }
            }).negativeText("Cancel").callback(new MaterialDialog.ButtonCallback() {
                @Override
                public void onNegative(MaterialDialog dialog) {
                    super.onNegative(dialog);
                    dialog.dismiss();
                }
            });
    addItemBuilder.neutralText("Add Quantity").callback(new MaterialDialog.ButtonCallback() {
        @Override
        public void onNeutral(final MaterialDialog dialog) {
            super.onNeutral(dialog);
            MaterialDialog.Builder quantityDialogBuilder = new MaterialDialog.Builder(MainActivity.this);
            quantityDialogBuilder.title("Add Quantity");
            quantityDialogBuilder.items(R.array.Quantaty_array);
            quantityDialogBuilder.itemsCallback(new MaterialDialog.ListCallback() {
                @Override
                public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                    choosenQuantity[0] = which+1;
                    addItemBuilder.content("Quantity: "+choosenQuantity[0]+"");
                }
            }).show();
        }
    }).show();
1

There are 1 best solutions below

2
On BEST ANSWER

In the code, on list selection callback, you are adjusting the MaterialDialog.Builder and not the dialog view.

addItemBuilder.content("Quantity: "+choosenQuantity[0]+"");

A Builder is just a tool for constructing the dialog view and once the view is shown with show() method call of the dialog, builder contents will not have an effect on the view. Try reshowing the dialog by dismissing the dialog and showing it again.

The other case without dismissing and reshowing is to find out the view where the text being displayed and directly setting the text.

Eg:

addItemBuilder.neutralText("Add Quantity").callback(new MaterialDialog.ButtonCallback() {
        @Override
        public void onNeutral(final MaterialDialog addItemDialog) {
            super.onNeutral(dialog);
            MaterialDialog.Builder quantityDialogBuilder = new MaterialDialog.Builder(MainActivity.this);
            quantityDialogBuilder.title("Add Quantity");
            quantityDialogBuilder.items(R.array.Quantaty_array);
            quantityDialogBuilder.itemsCallback(new MaterialDialog.ListCallback() {
                @Override
                public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                    choosenQuantity[0] = which+1;
                    TextView titleView = (TextView)addItemDialog.findViewById(R.id.dialog_title); // I assume the id as dialog_title

                    titleView.setText("Quantity: "+choosenQuantity[0]);

                }
            }).show();
        }
    }).show();