show progress dialog while loading data

谁说胖子不能爱 提交于 2019-12-23 02:18:22

问题


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 !!


回答1:


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



回答2:


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

Also, note that if you want to use Toast, you should use runOnUiThread




回答3:


If you do not want to change the structure of your code, you can use runOnUiThread or an Handler to show and dissmiss the progress dialog. Show it when the firs line of the run method is excuted and dismiss it in the finally block.

 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:


Create Progress Dialog in 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();


来源:https://stackoverflow.com/questions/16275913/show-progress-dialog-while-loading-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!