【问题标题】:show progress dialog while loading data加载数据时显示进度对话框
【发布时间】:2013-04-22 22:59:24
【问题描述】:

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

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

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

    }

请告诉我该怎么做!!

【问题讨论】:

  • 首先,您的 ProgressDialog 代码在哪里?我没有在上面的代码中找到它。如果您要求在上述课程中实现,请查看我的答案。

标签: android progressdialog


【解决方案1】:

如下创建类并调用该类的对象。

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

【讨论】:

    【解决方案2】:

    使用progressDialog

    final ProgressDialog progress=ProgressDialog.show(youractivity.this,"","message");
     new Thread()
      {
       public void run()
       {
         try{
          youractivity.this.runOnUiThread(new Runnable(){
          @Override
          public void run() {
           // TODO Auto-generated method stub
                   // your code
          }
          });
         }
         catch(Exception e)
         {
         }
         progress.dismiss();
       }
    }.start()
    

    另外,注意如果你想使用Toast,你应该使用runOnUiThread

    【讨论】:

      【解决方案3】:

      如果您不想更改代码的结构,可以使用runOnUiThread 或处理程序来显示和关闭进度对话框。在run 方法的第一行执行时显示它,并在finally 块中将其关闭。

       public void run() {
      
          runOnUiThread(new Runnable() {
      
            public void run(){
               // show progress dialog
            }  
      
            });  
      
        /// your code here
        try {
      
      
      
      
        } catch (IOException e) {
        } finally {
                runOnUiThread(new Runnable() {
      
            public void run(){
               // dismiss progress dialog
            }  
      
            });  
        } 
      
      
       }
      

      【讨论】:

        【解决方案4】:

        在 AsyncTask 中创建进度对话框

        private class YourAsyncTask extends AsyncTask<Void, Void, Void> {
             protected Void doInBackground(Void... args) {
                // do background work here
                return null;
             }
        
             protected void onPostExecute(Void result) {
                 // do UI work here
             }
         }
        

        【讨论】:

          【解决方案5】:
              pDialog = ProgressDialog.show(context, null, "Loading...", true);
              pDialog.setCancelable(false);
          
              new Thread() {
                  public void run() {
          
                      // handle the exception somehow, or do nothing
          
                      // run code on the UI thread
          
                      runOnUiThread(new Runnable() {
          
                          @Override
                          public void run() {
          
                              try {
          
          
                                           // do yor ui part here
                                   } catch (Exception e) {
          
                                  e.printStackTrace();
                              }
          
                          }
                      });
                  }
              }.start();
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-06-13
            • 1970-01-01
            • 2019-12-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多