从自定义对话框显示进度对话框

发布于 2024-12-19 22:53:53 字数 1802 浏览 1 评论 0原文

我试图在图像上传到服务器时从自定义对话框显示进度对话框。这就是我所遇到的:

final Dialog mDialog = new Dialog(this, R.style.CustomDialogTheme);
mDialog.setContentView(R.layout.sendimage_dialog);
ImageView im = (ImageView)mDialog.findViewById(R.id.image_to_upload);
                        im.setImageURI(Constants.currImageURI);

Button upload = (Button)mDialog.findViewById(R.id.upload);

upload.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View view) {
    mDialog.dismiss();

});

mDialog.show();

mDialog.setOnDismissListener(new OnDismissListener(){

@Override
    public void onDismiss(DialogInterface di) {

                                ProgressDialog progress = new ProgressDialog(CameraActivity.this, R.style.CustomDialogTheme);
                                progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                                progress.setMessage("Please wait while your photo is being uploaded.");
                                progress.show();

                                String response = uploadUserPhoto(new File(getRealPathFromURI(Constants.currImageURI)));
                                try {
                                    parseResponse(response);
                                } catch (JSONException e) {
                                    Log.d(TAG, e.getMessage());
                                    e.printStackTrace();
                                }

                                progress.dismiss();
                                Toast.makeText(getApplicationContext(), "Using your computer, log into your graFighters account and your image will be waiting for you.", Toast.LENGTH_SHORT).show();

                            }

                        });

问题是,当我点击上传按钮时,我的进度对话框永远不会显示。有什么想法可能是为什么吗?

I am trying to show a progress dialog from a custom dialog while an image get uploaded to a server. Here is what I have:

final Dialog mDialog = new Dialog(this, R.style.CustomDialogTheme);
mDialog.setContentView(R.layout.sendimage_dialog);
ImageView im = (ImageView)mDialog.findViewById(R.id.image_to_upload);
                        im.setImageURI(Constants.currImageURI);

Button upload = (Button)mDialog.findViewById(R.id.upload);

upload.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View view) {
    mDialog.dismiss();

});

mDialog.show();

mDialog.setOnDismissListener(new OnDismissListener(){

@Override
    public void onDismiss(DialogInterface di) {

                                ProgressDialog progress = new ProgressDialog(CameraActivity.this, R.style.CustomDialogTheme);
                                progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                                progress.setMessage("Please wait while your photo is being uploaded.");
                                progress.show();

                                String response = uploadUserPhoto(new File(getRealPathFromURI(Constants.currImageURI)));
                                try {
                                    parseResponse(response);
                                } catch (JSONException e) {
                                    Log.d(TAG, e.getMessage());
                                    e.printStackTrace();
                                }

                                progress.dismiss();
                                Toast.makeText(getApplicationContext(), "Using your computer, log into your graFighters account and your image will be waiting for you.", Toast.LENGTH_SHORT).show();

                            }

                        });

The problem is, when I hit the upload button, my progress dialog never shows. Any ideas why this might be?

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

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

发布评论

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

评论(3

蹲在坟头点根烟 2024-12-26 22:53:53

这是因为您在主 (UI) 线程中执行网络调用 (uploadUserPhoto),这意味着 UI 在调用 show() 和 dismiss() 之间不会更新。如果您希望此功能正常工作,您可以启动一个单独的工作线程来执行 uploadUserPhoto 操作。工作线程完成后,它必须向活动发出信号以删除对话框。

It is because you're performing the network call (uploadUserPhoto) in the main (UI) thread, which means that the UI won't be updated between the calls to show() and dismiss(). If you want this to work, you could launch a separate worker Thread that performs the uploadUserPhoto instead. After the worker Thread has finished, it must then signal the Activity to remove the dialog.

要走干脆点 2024-12-26 22:53:53

您正在单击方法中显示和关闭您的 ProgressDialog。至于

mDialog.dismss

它应该被驳回。如果您创建了自定义对话框,我们可能需要查看更多内容才能获得更好的想法。

编辑:然后调用对话框以在 onClick 方法之外显示。

You are showing and dismissing your ProgressDialog in the click method. As for

mDialog.dismss

It should be dismissing. If you created a custom dialog we may need to see more of it to get a better idea.

EDIT: and then your calling the dialog to show outside of your onClick method.

梦回旧景 2024-12-26 22:53:53

您应该使用AlertDialog。这是一个应该非常适合您的代码的示例:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Alert")
       .setMessage("Please wait while your photo is being uploaded.")
       .setNeutralButton("OK", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                   dialog.dismiss();
               })
       .show();

我还建议考虑使用 AsyncTaskProgressDialog

You should use an AlertDialog. Here is an example that should fit your code pretty well:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Alert")
       .setMessage("Please wait while your photo is being uploaded.")
       .setNeutralButton("OK", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                   dialog.dismiss();
               })
       .show();

I also recommend looking into using an AsyncTask and a ProgressDialog.

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