Android 进度对话框问题

发布于 2024-12-03 15:09:54 字数 2270 浏览 3 评论 0原文

在我的应用程序中,我尝试从服务器获取数据并将其存储在数据库中。 当它完成所有这些工作时,我希望当时进度对话框应该显示,如果成功获取数据,那么对话框应该关闭,并且一些警报对话框应该显示消息“成功获取数据”。如果存在任何 n/w 问题,那么它应该显示不同的消息“n/w 问题”。

为此,我正在执行如下所示的

public void onClick(View arg0) {
                myProgressDialog = ProgressDialog.show(
                        getParent(), "Please wait...",
                        "Doing upgrade...", true);
                new Thread() {
                    public void run() {
                        try {

                        upgradeAll();//function where data fetched from server

                            sleep(5000);

                        } catch (Exception e) {
                        }
                        // Dismiss the Dialog

                        myProgressDialog.dismiss();


                        Toast.makeText(UpgradeAllTableData.this, "Due to some internal problem \n" +
                                "it couldnot update..", Toast.LENGTH_LONG).show();
                    }

                }.start();
            }

AsyncTask 代码,

private class UpgradeTask extends AsyncTask<Context, Void, Void> {
        private ProgressDialog Dialog = new ProgressDialog(UpgradeAllTableData.this);

        @Override
        protected void onPreExecute() {
            System.out.println("In onPreExecute ");
            Dialog.setTitle("Loading");
            Dialog.setMessage("Please wait for few seconds...");
            Dialog.show();
        }

        @Override
        protected Void doInBackground(Context... params) {
            System.out.println("In doInBackground ");

            upgradeAll();

            System.out.println("In doInBackground after fetching");
            return null;
        }

        @Override
          protected void onPostExecute(Void unused) {

            System.out.println("In onPostExecute ");
            Dialog.dismiss();

            Toast.makeText(UpgradeAllTableData.this, "Problem with internet" ,  Toast.LENGTH_SHORT).show();
            alertboxNeutral("Warning", "Problem with Internet Connection", "Okay","Try again");

        }
    }

问题是 Toast 未显示。为什么?

我的问题是给出哪个条件以及在哪里给出,以便如果 n/w 出现任何问题,那么它会显示一些消息并成功,然后显示另一个消息。

我应该在哪里编写代码? 给我一些建议。

谢谢

In my app i m trying to fetch data from server and storing in database.
When it is doing all these work i want at that time progressdialog should show, if successfully data fetches then dialog should close and some alertDialog should show for msg "successfully data fetched". and if any n/w problem there, then it should show different msg that "problem with n/w".

for that i am doing like below,

public void onClick(View arg0) {
                myProgressDialog = ProgressDialog.show(
                        getParent(), "Please wait...",
                        "Doing upgrade...", true);
                new Thread() {
                    public void run() {
                        try {

                        upgradeAll();//function where data fetched from server

                            sleep(5000);

                        } catch (Exception e) {
                        }
                        // Dismiss the Dialog

                        myProgressDialog.dismiss();


                        Toast.makeText(UpgradeAllTableData.this, "Due to some internal problem \n" +
                                "it couldnot update..", Toast.LENGTH_LONG).show();
                    }

                }.start();
            }

AsyncTask code,

private class UpgradeTask extends AsyncTask<Context, Void, Void> {
        private ProgressDialog Dialog = new ProgressDialog(UpgradeAllTableData.this);

        @Override
        protected void onPreExecute() {
            System.out.println("In onPreExecute ");
            Dialog.setTitle("Loading");
            Dialog.setMessage("Please wait for few seconds...");
            Dialog.show();
        }

        @Override
        protected Void doInBackground(Context... params) {
            System.out.println("In doInBackground ");

            upgradeAll();

            System.out.println("In doInBackground after fetching");
            return null;
        }

        @Override
          protected void onPostExecute(Void unused) {

            System.out.println("In onPostExecute ");
            Dialog.dismiss();

            Toast.makeText(UpgradeAllTableData.this, "Problem with internet" ,  Toast.LENGTH_SHORT).show();
            alertboxNeutral("Warning", "Problem with Internet Connection", "Okay","Try again");

        }
    }

Problem is Toast is not showing. why?

My question is which condition and where to give so that if any problem with n/w then it show some msg and success then show another msg.

Where i should write code for that?
Give me some suggestion.

Thank you

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

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

发布评论

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

评论(2

挽梦忆笙歌 2024-12-10 15:09:54

不完全是您特定问题的答案,但您考虑过 AsyncTask 吗?它几乎是为处理像您这样的情况而设计的,其中任务是异步执行的,同时在 UI 线程上显示一些进度(和最终结果)。或者,您可以广播一个意图并让您的 Activity 捕获它以显示 Toast,因为您的 Toast 也应该从您的 UI 线程中显示。

更新:
AsyncTask 参考 - http://developer.android.com/reference/android/os/ AsyncTask.html

Not exactly an answer to your particular question, but have you considered AsyncTask? It's pretty much designed to handle situations like yours, where a task is performed async while showing some progress (and eventual result) on the UI thread. Alternativelly, you could broadcast an intent and have your activity catch it to show the toast, since your Toast should be show from your UI thread as well.

update:
AsyncTask reference - http://developer.android.com/reference/android/os/AsyncTask.html

╰ゝ天使的微笑 2024-12-10 15:09:54

你所做的事情是完全错误的。 UI 线程处理所有 UI 更改,但在这里您在 UI 线程中创建 ProgressDialog 并在其他线程中将其关闭。解决方案是使用 AsyncTask http://developer.android.com/reference/android/os/AsyncTask.html

What you are doing is totally wrong. UI thread handles all UI changes, but here you are creating ProgressDialog in UI thread and dismissing it in some Other Thread.. A solution is make use of AsyncTask http://developer.android.com/reference/android/os/AsyncTask.html

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