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

时间:2011-12-06 15:07:08

标签: android dialog progressdialog

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

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

                            }

                        });

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

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

您正在点击方法中显示和解除ProgressDialog。至于

mDialog.dismss

应该是解雇。如果您创建了自定义对话框,我们可能需要查看更多内容以获得更好的想法。

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

答案 2 :(得分:0)

您应该使用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