加载数据时显示进度对话框 [英] show progress dialog while loading data

查看:54
本文介绍了加载数据时显示进度对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在从远程服务器加载一些数据时显示一个进度对话框:

I want to show a progress dialog while loading some data from remote server :

我正在使用以下线程来获取数据并且它正在工作,但我无法在活动中显示进度条:

I'm using the following thread in order to get the data and it's working, but i'm not able to show the progress bar on the activity:

public class Request {

    public String text ;
    public boolean downloadText(String urlStr) {
        final String url = urlStr;
        new Thread () {
            public void run() {
                int BUFFER_SIZE = 2000;
                InputStream in = null;
                Message msg = Message.obtain();
                msg.what=2;
                try {
                    in = openHttpConnection(url);

                    InputStreamReader isr = new InputStreamReader(in);
                    int charRead;
                      text = "";
                      char[] inputBuffer = new char[BUFFER_SIZE];

                          while ((charRead = isr.read(inputBuffer))>0)
                          {                    
                              //---convert the chars to a String---
                              String readString = 
                                  String.copyValueOf(inputBuffer, 0, charRead);                    
                              text += readString;
                              inputBuffer = new char[BUFFER_SIZE];
                          }
                         Bundle b = new Bundle();
                            b.putString("text", text);
                            msg.setData(b);
                          in.close();

                }catch (IOException e) {
                    e.printStackTrace();
                }


            }
        }.start();

    }

你能告诉我我该怎么做吗!!

would you please tell me how can i do it !!

推荐答案

创建如下类,调用该类的对象即可.

create the class as below and just call the object of this class.

class MyTask extends AsyncTask<Void, Void, Void> {

        ProgressDialog Asycdialog = new ProgressDialog(ActivityName.this);

        @Override
        protected void onPreExecute() {

            super.onPreExecute();
            Asycdialog.setMessage("Loading...");
            Asycdialog.show();
        }

        @Override
        protected Void doInBackground(Void... arg0) {

            // do the task you want to do. This will be executed in background.
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            super.onPostExecute(result);
            Asycdialog.dismiss();
        }
    }

这篇关于加载数据时显示进度对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆