执行dismiss、隐藏或取消后ProgressDialog不消失?

发布于 2024-10-10 21:13:07 字数 1899 浏览 3 评论 0原文

我有一个 Overlay 扩展,它有 2 个对话框作为私有属性 - 一个 Dialog 和一个 ProgressDialog。单击 MapView 中的 Overlay 后,将出现 Dialog 对象。当用户单击对话框中的按钮时,它会消失并显示 ProgressDialog。同时,通过通知正在运行的服务来启动后台任务。任务完成后,将调用 Overlay 对象中的方法 (buildingLoaded) 来切换视图并关闭 ProgressDialog。视图正在切换,代码正在运行(我用调试器检查过),但 ProgressDialog 没有被关闭。我也尝试过 hide() 和 cancel() 方法,但没有任何作用。有人可以帮助我吗? Android版本是2.2

这是代码:

public class LODOverlay extends Overlay implements OnClickListener {



private Dialog overlayDialog;

private ProgressDialog progressDialog;

       ..............


@Override
public void onClick(View view) {

                   .......

        final Context ctx = view.getContext();
        this.progressDialog = new ProgressDialog(ctx);
        ListView lv = new ListView(ctx);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(ctx, R.layout.layerlist, names);
        lv.setAdapter(adapter);
        final LODOverlay obj = this;
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
                String name = ((TextView) view).getText().toString();
        Intent getFloorIntent = new Intent(Map.RENDERER);
        getFloorIntent.putExtra("type", "onGetBuildingLayer");
        getFloorIntent.putExtra("id", name);
        view.getContext().sendBroadcast(getFloorIntent);
        overlayDialog.dismiss();

        obj.waitingForLayer = name;

        progressDialog.show(ctx, "Loading...", "Wait!!!");


            }
        });

    .......
}


public void buildingLoaded(String id) {
    if (null != this.progressDialog) {
        if (id.equals(this.waitingForLayer)) {
            this.progressDialog.hide();
            this.progressDialog.dismiss();

    ............

            Map.flipper.showNext();  // changes the view
        }
    }
}

}

I have an Overlay extension which has 2 dialogs as private attributes - one Dialog and one ProgressDialog. After clicking on the Overlay in the MapView, the Dialog object appears. When the user clicks a button in the Dialog it disappears and the ProgressDialog is shown. Simultaneously a background task is started by notifying a running Service. When the task is done, a method (buildingLoaded) in the Overlay object is called to switch the View and to dismiss the ProgressDialog. The View is being switched, the code is being run (I checked with the debugger) but the ProgressDialog is not dismissed. I also tried hide() and cancel() methods, but nothing works. Can somebody help me?
Android version is 2.2

Here is the code:

public class LODOverlay extends Overlay implements OnClickListener {



private Dialog overlayDialog;

private ProgressDialog progressDialog;

       ..............


@Override
public void onClick(View view) {

                   .......

        final Context ctx = view.getContext();
        this.progressDialog = new ProgressDialog(ctx);
        ListView lv = new ListView(ctx);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(ctx, R.layout.layerlist, names);
        lv.setAdapter(adapter);
        final LODOverlay obj = this;
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
                String name = ((TextView) view).getText().toString();
        Intent getFloorIntent = new Intent(Map.RENDERER);
        getFloorIntent.putExtra("type", "onGetBuildingLayer");
        getFloorIntent.putExtra("id", name);
        view.getContext().sendBroadcast(getFloorIntent);
        overlayDialog.dismiss();

        obj.waitingForLayer = name;

        progressDialog.show(ctx, "Loading...", "Wait!!!");


            }
        });

    .......
}


public void buildingLoaded(String id) {
    if (null != this.progressDialog) {
        if (id.equals(this.waitingForLayer)) {
            this.progressDialog.hide();
            this.progressDialog.dismiss();

    ............

            Map.flipper.showNext();  // changes the view
        }
    }
}

}

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

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

发布评论

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

评论(2

一影成城 2024-10-17 21:13:07

不确定这是否是问题的原因,但您在 ProgressDialog 上调用的方法是 static,但您是在该类的实例上调用它。这是方法定义:

public static ProgressDialog show (Context context, CharSequence title, CharSequence message)

如您所见,该方法返回一个ProgressDialog,它不会对您的类实例执行show操作。更新您的代码以使用以下之一:

progressDialog.setTitle("Loading...");
progressDialog.setMessage("Wait!!!");
progressDialog.show();

progressDialog = ProgressDialog.show(ctx, "Loading...", "Wait!!!");

Not sure if this is the cause of your issue, but the method you are calling on ProgressDialog is static, but you are calling it on an instance of the class. Here's the method definition:

public static ProgressDialog show (Context context, CharSequence title, CharSequence message)

As you can see, the method returns a ProgressDialog, it does not perform the show operation on your instance of the class. Update your code to use one of these:

progressDialog.setTitle("Loading...");
progressDialog.setMessage("Wait!!!");
progressDialog.show();

or

progressDialog = ProgressDialog.show(ctx, "Loading...", "Wait!!!");
来世叙缘 2024-10-17 21:13:07

ProgressDialog.show(...) 方法实际上在返回对话框之前执行 show() 操作。 Android.jar 源码如下:

public static ProgressDialog show(Context context, CharSequence title,
        CharSequence message, boolean indeterminate,
        boolean cancelable, OnCancelListener cancelListener) {
    ProgressDialog dialog = new ProgressDialog(context);
    dialog.setTitle(title);
    dialog.setMessage(message);
    dialog.setIndeterminate(indeterminate);
    dialog.setCancelable(cancelable);
    dialog.setOnCancelListener(cancelListener);
    dialog.show();
    return dialog;
}

该方法的所有重载均引用此方法。

ProgressDialog.show(...) methods do in fact perform a show() on the dialog before returning it. Here is Android.jar source:

public static ProgressDialog show(Context context, CharSequence title,
        CharSequence message, boolean indeterminate,
        boolean cancelable, OnCancelListener cancelListener) {
    ProgressDialog dialog = new ProgressDialog(context);
    dialog.setTitle(title);
    dialog.setMessage(message);
    dialog.setIndeterminate(indeterminate);
    dialog.setCancelable(cancelable);
    dialog.setOnCancelListener(cancelListener);
    dialog.show();
    return dialog;
}

All the overloads of this method refer to this one.

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