没有显示来自其他非活动类的AlertDialog

时间:2019-04-02 06:42:29

标签: java android alertdialog

我正在尝试显示另一个班级的警报对话框。我在StackOverflow中查找了许多问题,但没有一个起作用。

我有两个班级MainActivity.javaCustomInputDialog.java。我正在尝试显示CustomInputDialog.java中指定的MainActivity.java的警报对话框。

在我的MainActivity.java中,我有以下代码:

ArrayList<CustomInputDialog.Field> fields = new ArrayList<>(Arrays.asList(
                        new CustomInputDialog.Field(CustomInputDialog.Field.TYPE.TEXT, "Name", "", null),
                        new CustomInputDialog.Field(CustomInputDialog.Field.TYPE.DATE, "Start Date", "", null),
                        new CustomInputDialog.Field(CustomInputDialog.Field.TYPE.DATE, "End Date", "", null)
                ));

                ArrayList<String> result = CustomInputDialog.showDialog(MainActivity.this, "Title", fields);

在我的CustomInputDialog.java中,我有以下代码:

final class CustomInputDialog {
    private static final String TAG = "CustomInputDialog";
    private static final String dateUISeparator = " : ";

    static ArrayList<String> showDialog(final Context context, String title, final ArrayList<Field> fields) {
        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
        final LinearLayout layout = new LinearLayout(context);
        layout.setOrientation(LinearLayout.VERTICAL);
        final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(10, 10, 10, 10);
        final ArrayList<View> uis = new ArrayList<>();
        for (final Field field : fields) {
            final View ui;
            switch (field.type) {
                 /**To long code it just creates specified views and saves it in `ui` variable*/
            }
            ui.setLayoutParams(layoutParams);
            Log.d(TAG, "showDialog: ui added");
            layout.addView(ui);
            uis.add(ui);
        }
        alertDialog.setTitle(title);
        alertDialog.setView(layout);

        final ArrayList<String> result = new ArrayList<>();
        Log.d(TAG, "showDialog: latch created");
        final CountDownLatch latch = new CountDownLatch(1);
        alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                /** Long code which updates `result` variable */
                dialog.dismiss();
                latch.countDown();
            }
        });
        alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                latch.countDown();
                Log.d(TAG, "showDialog: latch count down 2");
            }
        });
        alertDialog.setCancelable(false);
        alertDialog.show();
        Log.d(TAG, "showDialog: showing");
        try {
            Log.d(TAG, "showDialog: latch await");
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            if (result.isEmpty()) {
                return null;
            } else {
                return result;
            }
        }
    }

    static final class Field {
        private final TYPE type;
        private final String helpText;
        private final String initialValue;
        private final int choice;

        enum TYPE {
            TEXT, DATE, DOUBLE, INTEGER, CHOICE
        }

        Field(TYPE type, String helpText, String initialValue, @Nullable Integer choice) {
            this.type = type;
            this.helpText = helpText;
            this.initialValue = initialValue;
            if (choice == null) {
                this.choice = 0;
            } else {
                this.choice = choice;
            }
        }
    }
}

在调试过程中,结果发现没有异常抛出,但是方法alertDialog中的变量showDialog仍然不可见。

系统输出:

D/CustomInputDialog: showDialog: ui created : editText
D/CustomInputDialog: showDialog: ui added
D/CustomInputDialog: showDialog: ui created : date
D/CustomInputDialog: showDialog: ui added
D/CustomInputDialog: showDialog: ui created : date
D/CustomInputDialog: showDialog: ui added
D/CustomInputDialog: showDialog: latch created
D/CustomInputDialog: showDialog: showing
D/CustomInputDialog: showDialog: latch await

1 个答案:

答案 0 :(得分:0)

问题似乎来自阻止线程的CountDownLatch,您应该在自定义对话框类中创建一个接口,在其中您可以在其他地方实现onResult()(或您想调用的名称)。 / p>

您将必须将一个侦听器传递给实现showDialog()的{​​{1}},并在“确定”按钮onResult()中调用onClick

接口(在CustomInputDialog.java中):

listener.onResult()

要传递给showDialog的新参数:

interface CustomInputDialogListener{
    void onResult(ArrayList<String> result);
}

确定按钮onClick()的结尾:

static void showDialog(final Context context, String title, final ArrayList<Field> fields, final CustomInputDialogListener listener) {
...
}

您可以删除结尾处带有wait和return语句的闩锁创建和try / catch块

dialog.dismiss();
listener.onResult(result);
//latch.countDown(); //you don't need that anymore
Log.d(TAG, "showDialog: latch count down 1");

在MainActivity.java中,您有2个选择:

  

实现CustomInputDialogListener并将其传递给//Log.d(TAG, "showDialog: latch created"); //final CountDownLatch latch = new CountDownLatch(1); /*try { Log.d(TAG, "showDialog: latch await"); latch.await(); } catch (InterruptedException e) { e.printStackTrace(); } finally { if (result.isEmpty()) { return null; } else { return result; } }*/

MainActivity的标题应如下所示:

showDialog

并且您将必须实现onResult():

public class MainActivity extends AppCompatActivity implements CustomInputDialog.CustomInputDialogListener {
    ...
}

当您调用showDialog()时,将传递此信息:

@Override
public void onResult(ArrayList<String> result) {
    this.result = result;
    doThings();
}
  

您直接实现onResult:

CustomInputDialog.showDialog(Test14dialog.this, "Title", fields, this);

显示对话框时,您不应该阻止线程