即使我在 Android 上调用 onCreateDialog,AlertDialog 也不会创建

发布于 2024-12-02 21:09:02 字数 3078 浏览 0 评论 0原文

我面临以下问题。

我有一个图像Gallery,我想打开一个AlertDialog来显示有关单击图像的一些信息。我第一次点击图像时,它运行得很好。但接下来的几次,即使我单击不同的图像,也会再次出现相同的对话框。

UserCase

当我按照此用户案例

  1. 在位置 2 选择图像
  2. 关闭对话框
  3. 在位置 6
  4. 关闭对话框

选择图像时,我得到这些打印:

click na galeria... position: 2
startUserInformationDialog()... `this.position`: 2
onCreateDialog on switch.. `this.position`: 2
DISMISSING DIALOG `this.position`: 2

click na galeria... position: 6
startUserInformationDialog()... `this.position`: 6
DISMISSING DIALOG `this.position`: 6

这意味着代码没有通过 onCreateDialog

代码运行

public AlertDialog createUserInformationAlertDialog() {
        LayoutInflater inflater = getLayoutInflater();
        View dialoglayout = inflater.inflate(R.layout.dialog_user_info,
                (ViewGroup) findViewById(R.id.dialog_user_layout_root));
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(dialoglayout);
        final MyObject ea = myList.get(positionUserToHaveInformationDisplayedOnTheDialog);
            // Setting values according to element to be shown
            TextView textView = (TextView) dialoglayout
                    .findViewById(R.id.user_name_value);
            textView.setText(ea.getName());
            ...
            builder.setNegativeButton(Locale_PT_BR.BACK,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            Log.d("PRINT 1",
                                    "DISMISSING DIALOG `this.position`: "
                                            + positionUserToHaveInformationDisplayedOnTheDialog);
                            dialog.dismiss();
                        }
                    });
            return builder.create();
}


private void startUserInformationDialog() {
    Log.d("PRINT 2", "startUserInformationDialog()... `this.position`: "
            + positionUserToHaveInformationDisplayedOnTheDialog);
    showDialog(DIALOG_USER_INFORMATION);
}



@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DIALOG_USER_INFORMATION:
            Log.d("PRINT 3", "onCreateDialog on switch.. `this.position`: "
                    + positionUserToHaveInformationDisplayedOnTheDialog);
            return createUserInformationAlertDialog();
        default:
            return null;
        }
    }

private void startGallery() {
        myPhotoBar = (Gallery) findViewById(R.id.gallery);
        myPhotoBar.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position,
                    long id) {
                Log.d("PRINT 4", "click na galeria... position: " + position);
                positionUserToHaveInformationDisplayedOnTheDialog = position;
                startUserInformationDialog();
            }
        });

}

I am facing the following problem.

I have an image Gallery and I want to open an AlertDialog to display some information regarding to the clicked image. The first time I click on the image, it runs just fine. But the following times, even though I click on a different image, the same dialog comes up again.

UserCase

When I follow this User Case

  1. Choose an image in position 2
  2. Close the Dialog
  3. Choose an Image in position 6
  4. Close the Dialog

, I get these prints:

click na galeria... position: 2
startUserInformationDialog()... `this.position`: 2
onCreateDialog on switch.. `this.position`: 2
DISMISSING DIALOG `this.position`: 2

click na galeria... position: 6
startUserInformationDialog()... `this.position`: 6
DISMISSING DIALOG `this.position`: 6

It means that the code is not running through the onCreateDialog

CODE

public AlertDialog createUserInformationAlertDialog() {
        LayoutInflater inflater = getLayoutInflater();
        View dialoglayout = inflater.inflate(R.layout.dialog_user_info,
                (ViewGroup) findViewById(R.id.dialog_user_layout_root));
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(dialoglayout);
        final MyObject ea = myList.get(positionUserToHaveInformationDisplayedOnTheDialog);
            // Setting values according to element to be shown
            TextView textView = (TextView) dialoglayout
                    .findViewById(R.id.user_name_value);
            textView.setText(ea.getName());
            ...
            builder.setNegativeButton(Locale_PT_BR.BACK,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            Log.d("PRINT 1",
                                    "DISMISSING DIALOG `this.position`: "
                                            + positionUserToHaveInformationDisplayedOnTheDialog);
                            dialog.dismiss();
                        }
                    });
            return builder.create();
}


private void startUserInformationDialog() {
    Log.d("PRINT 2", "startUserInformationDialog()... `this.position`: "
            + positionUserToHaveInformationDisplayedOnTheDialog);
    showDialog(DIALOG_USER_INFORMATION);
}



@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DIALOG_USER_INFORMATION:
            Log.d("PRINT 3", "onCreateDialog on switch.. `this.position`: "
                    + positionUserToHaveInformationDisplayedOnTheDialog);
            return createUserInformationAlertDialog();
        default:
            return null;
        }
    }

private void startGallery() {
        myPhotoBar = (Gallery) findViewById(R.id.gallery);
        myPhotoBar.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position,
                    long id) {
                Log.d("PRINT 4", "click na galeria... position: " + position);
                positionUserToHaveInformationDisplayedOnTheDialog = position;
                startUserInformationDialog();
            }
        });

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北城挽邺 2024-12-09 21:09:02

使用 onPrepareDialog() 方法用数据填充对话框。如果您希望销毁对话框,您应该调用 removeDialog() 方法。

Use onPrepareDialog() method to fill dialog with data. If you want dialog to be destroyed you should call removeDialog() method.

つ低調成傷 2024-12-09 21:09:02

我相信,如果 ID 相同,则 showDialog 仅调用 onCreateDialog 一次。如果您想更新已创建的对话框,您可以覆盖 onPrepareDialog 以在向用户显示之前更新您想要的任何内容。

I believe that showDialog only calls onCreateDialog once, if the ID is the same. If you want to update an dialog which has already been created you can override onPrepareDialog to update whatever you like before it is shown to the user.

月寒剑心 2024-12-09 21:09:02

没关系,因为如果您看到 doc,则 onCreateDialog 是第一时间打电话来。

“当第一次请求对话框时,Android 会从您的 Activity 中调用 onCreateDialog(int),您应该在其中实例化对话框。此回调方法将传递给创建对话框后,在方法末尾返回该对象。”

您需要知道这将被创建一次。

调用onPrepareDialog

“在显示对话框之前,Android 还会调用可选的回调方法 onPrepareDialog(int, Dialog)。如果要在每次打开对话框时更改对话框的任何属性,请定义此方法每次打开对话框时都会调用此方法而 onCreateDialog(int) 仅在第一次打开对话框时调用,如果未定义 onPrepareDialog(),则。对话框将保持不变与上次打开时一样。此方法还会传递对话框的 ID 以及您在 onCreateDialog() 中创建的 Dialog 对象。”

And is ok because if you see the doc, the onCreateDialog is call in first time.

"When a dialog is requested for the first time, Android calls onCreateDialog(int) from your Activity, which is where you should instantiate the Dialog. This callback method is passed the same ID that you passed to showDialog(int). After you create the Dialog, return the object at the end of the method."

You need to know that this will be created once.

Call onPrepareDialog.

"Before the dialog is displayed, Android also calls the optional callback method onPrepareDialog(int, Dialog). Define this method if you want to change any properties of the dialog each time it is opened. This method is called every time a dialog is opened, whereas onCreateDialog(int) is only called the very first time a dialog is opened. If you don't define onPrepareDialog(), then the dialog will remain the same as it was the previous time it was opened. This method is also passed the dialog's ID, along with the Dialog object you created in onCreateDialog()."

过气美图社 2024-12-09 21:09:02

你可以尝试这个简单的android对话框弹出库。在您的活动中使用它非常简单。

如果您不需要复杂的功能,只需一行代码就足以创建对话框。

Pop.on(activity).with().title(R.string.title).body(R.string.body).show();

只需在您的 gradle 依赖项中包含以下库即可

dependencies {
    compile 'com.vistrav:pop:2.0'
}

You can try this simple android dialog popup library. It is very simple to use on your activity.

just one line of code is enough to create dialog if you dont need complex functionality around.

Pop.on(activity).with().title(R.string.title).body(R.string.body).show();

Just include following lib in your gradle dependencies

dependencies {
    compile 'com.vistrav:pop:2.0'
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文