0

Here is my simple AsyncTask :

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

    @Override
    protected String doInBackground(String... params) {

        String page = null;
        try {
            page = new Communicator().executeHttpGet(params[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return page;
    }

how do I add a progress bar to this that displays a simple spinner? Every time I try the code breaks or parameters are misplaced.

EDIT:

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

    ProgressDialog myPd_ring = null;

    @Override
    protected void onPreExecute() {


        myPd_ring.show();

    }
    @Override
    protected String doInBackground(String... params) {

        String page = null;
        try {
            page = new Communicator().executeHttpGet(params[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return page;

    }

    @Override
    protected void onPostExecute(String result) {
        myPd_ring.dismiss();        
        super.onPostExecute(result);
    }

}

Tried this still doesn't work ! In fact the application isn't even responding to standard input now ! Thanks !

Anurupa_Dey
  • 113
  • 4
  • 11

2 Answers2

1
@Override
        protected void onPreExecute() {

            myPd_ring  = new ProgressDialog (YourActivityName.this);
            myPd_ring.setMessage("Forza Roma");
            myPd_ring.show();

        }

Also change your class to

class MyTask extends AsyncTask<String, Integer, String>

and on onPostExecute function you should call the super function first like this:

 @Override
    protected void onPostExecute(String result) {        
        super.onPostExecute(result);
        myPd_ring.dismiss();
}

Furthermore, for your business logic, you should call new AsyncTask in the onPostExecute not on doInBackground

William Kinaan
  • 28,059
  • 20
  • 85
  • 118
  • @WilliamKinnan Shows The constructor ProgressDialog() is undefined – Anurupa_Dey Jul 02 '13 at 19:39
  • you need to import `android.app.ProgressDialog` – Moog Jul 02 '13 at 19:41
  • @Anurupa_Dey I just forget to tell you that the constructor should take the YourActivityName.this as a parameter, check the update please – William Kinaan Jul 02 '13 at 19:42
  • @WilliamKinaan Please explain :( Sorry but am just a beginner ! – Anurupa_Dey Jul 02 '13 at 19:44
  • 1
    check my update, the ProgressDialog class take an attribute which is the context that you are working on, you can get the context by typing `yourActivityName.this`, lets say your activity name is `Player` so the the constructor will be `new ProgressDialgo(Player.this);` – William Kinaan Jul 02 '13 at 19:45
  • however, you could also say `new ProgressDialog(getApplicationContext())` – William Kinaan Jul 02 '13 at 19:47
  • @WilliamKinaan My class myTask is defined in the MainActivity.java below the class MainActivity. It's not on a separate file. So whenever I try to do this.mainActivity it shows: MainActivity cannot be resolved or is not a field. I tried this: `ProgressDialog myPd_ring; @Override protected void onPreExecute() { myPd_ring = new ProgressDialog(this.MainActivity); myPd_ring.setMessage("Getting Data"); myPd_ring.show();` that produced the error. – Anurupa_Dey Jul 02 '13 at 19:55
  • @Anurupa_Dey you should do this: `myPd_ring = new ProgressDialog(MainActivity.this);` – William Kinaan Jul 02 '13 at 19:57
  • @WilliamKinaan shows the error No enclosing instance of the type MainActivity is accessible in scope – Anurupa_Dey Jul 02 '13 at 19:59
  • that code contains many errors, first, you should do this `String page = new MyTask().execute(url);` without get, second, the code after it should be in the `onPostExecute`, third the `myPd_ring = new ProgressDialog( );` should be `myPd_ring = new ProgressDialog( MainActivity.this);`, also there are many problems, please split your code to pieces of codes and try every one alone. – William Kinaan Jul 02 '13 at 20:12
0

override the method onPreExecute()

in that method create a ProgressDialog and show it. You can dismiss the ProgressDialog in the method onPostExecute() that you should also override. Check out more about the AsyncTask here

Be careful using AsyncTasks though, it has some hidden pitfalls. Check out this article about it.

dumazy
  • 13,857
  • 12
  • 66
  • 113
  • tried this: `class MyTask extends AsyncTask { ProgressDialog myPd_ring = null; @Override protected void onPreExecute() { myPd_ring.show(); } @Override protected String doInBackground(String... params) { String page = null; try { page = new Communicator().executeHttpGet(params[0]); } catch (Exception e) { e.printStackTrace(); } return page; } @Override protected void onPostExecute(String result) { myPd_ring.dismiss(); super.onPostExecute(result); } }` Doesn't work and broke the code :( – Anurupa_Dey Jul 02 '13 at 19:20
  • of course, myPd_ring is still null and you try to show it. add myPd_ring = new ProgressDialog() (or something like that) before myPd_ring.show() – dumazy Jul 02 '13 at 19:59