如何更新对话框的内容

时间:2015-06-20 18:56:02

标签: android android-dialog

我尝试在用户从“列表”对话框中选择“数量”后更新对话框的内容。例如,现在向用户显示我的"添加项目对话框": enter image description here

并且默认数量为1,但是如果用户决定他/她想要更多按下添加数量(中性)按钮,则会显示另一个对话框: enter image description here 问题是,在用户选择数量后,我的代码不会按照我的意愿刷新第一个对话框的内容,它仍然会显示1。 顺便说一下,我正在使用第3方library来创建对话框 这是我的代码:

  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 个答案:

答案 0 :(得分:0)

在代码中,在列表选择回调中,您正在调整MaterialDialog。构建器,而不是对话框视图。

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

构建器只是构建对话框视图的工具,一旦通过对话框的show()方法调用显示视图,构建器内容将不会对视图产生影响。尝试通过关闭对话框并再次显示来重新显示对话框。

没有解雇和重新展示的另一种情况是找出正在显示文本的视图并直接设置文本。

例如:

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