Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ProgressDialog in AsyncTask

I'm trying to display a custom progressdialog while loading RSS feed from an HTTP server, I made a hard search, but nothing helped me to do this, the only thing I know is that the solution should use AsyncTask, but I'm confusing about the params to pass to this AsyncTask. Here's my activity :

public class Soirees extends ListActivity {      private List<Message> messages;     private TextView tvSorties;     private MyProgressDialog dialog;      @Override     public void onCreate(Bundle icicle) {          super.onCreate(icicle);         setContentView(R.layout.sorties);          tvSorties=(TextView)findViewById(R.id.TVTitle);         tvSorties.setText("Programme des soirées");          loadFeed();      }      private void loadFeed(){          try{             BaseFeedParser parser = new BaseFeedParser();             messages = parser.parse();             List<Message> titles = new ArrayList<Message>(messages.size());             for (Message msg : messages){                 titles.add(msg);             }             MessageListAdapter adapter = new MessageListAdapter(this,titles);             this.setListAdapter(adapter);             adapter.notifyDataSetChanged();          } catch (Throwable t){             Log.e("ImageLoader",t.getMessage(),t);         }     }  } 

Can you please help me add AsyncTask to this?

like image 289
Houssem Avatar asked Dec 27 '10 11:12

Houssem


People also ask

What are the three generic types of an AsyncTask?

An asynchronous task is defined by 3 generic types, called Params , Progress and Result , and 4 steps, called onPreExecute , doInBackground , onProgressUpdate and onPostExecute .

Which method in AsyncTask is run in a background thread?

doInBackground() : This method contains the code which needs to be executed in background.

How many times an instance of AsyncTask can be executed?

AsyncTask instances can only be used one time.


2 Answers

/**  * this class performs all the work, shows dialog before the work and dismiss it after  */ public class ProgressTask extends AsyncTask<String, Void, Boolean> {      public ProgressTask(ListActivity activity) {         this.activity = activity;         dialog = new ProgressDialog(activity);     }      /** progress dialog to show user that the backup is processing. */     private ProgressDialog dialog;     /** application context. */     private ListActivity activity;      protected void onPreExecute() {         this.dialog.setMessage("Progress start");         this.dialog.show();     }          @Override     protected void onPostExecute(final Boolean success) {         if (dialog.isShowing()) {             dialog.dismiss();         }           MessageListAdapter adapter = new MessageListAdapter(activity, titles);         setListAdapter(adapter);         adapter.notifyDataSetChanged();           if (success) {             Toast.makeText(context, "OK", Toast.LENGTH_LONG).show();         } else {             Toast.makeText(context, "Error", Toast.LENGTH_LONG).show();         }     }      protected Boolean doInBackground(final String... args) {        try{               BaseFeedParser parser = new BaseFeedParser();           messages = parser.parse();           List<Message> titles = new ArrayList<Message>(messages.size());           for (Message msg : messages){               titles.add(msg);           }           activity.setMessages(titles);           return true;        } catch (Exception e)           Log.e("tag", "error", e);           return false;        }     } }  public class Soirees extends ListActivity {     private List<Message> messages;     private TextView tvSorties;     private MyProgressDialog dialog;     @Override     public void onCreate(Bundle icicle) {          super.onCreate(icicle);          setContentView(R.layout.sorties);          tvSorties=(TextView)findViewById(R.id.TVTitle);         tvSorties.setText("Programme des soirées");          // just call here the task         AsyncTask task = new ProgressTask(this).execute();    }     public void setMessages(List<Message> msgs) {       messages = msgs;    }  } 
like image 136
Vladimir Ivanov Avatar answered Sep 24 '22 05:09

Vladimir Ivanov


Fixed by moving the view modifiers to onPostExecute so the fixed code is :

public class Soirees extends ListActivity {     private List<Message> messages;     private TextView tvSorties;      //private MyProgressDialog dialog;     @Override     public void onCreate(Bundle icicle) {          super.onCreate(icicle);          setContentView(R.layout.sorties);          tvSorties=(TextView)findViewById(R.id.TVTitle);         tvSorties.setText("Programme des soirées");          new ProgressTask(Soirees.this).execute();      }       private class ProgressTask extends AsyncTask<String, Void, Boolean> {         private ProgressDialog dialog;         List<Message> titles;         private ListActivity activity;         //private List<Message> messages;         public ProgressTask(ListActivity activity) {             this.activity = activity;             context = activity;             dialog = new ProgressDialog(context);         }            /** progress dialog to show user that the backup is processing. */          /** application context. */         private Context context;          protected void onPreExecute() {             this.dialog.setMessage("Progress start");             this.dialog.show();         }              @Override         protected void onPostExecute(final Boolean success) {                 List<Message> titles = new ArrayList<Message>(messages.size());                 for (Message msg : messages){                     titles.add(msg);                 }                 MessageListAdapter adapter = new MessageListAdapter(activity, titles);                 activity.setListAdapter(adapter);                 adapter.notifyDataSetChanged();                  if (dialog.isShowing()) {                 dialog.dismiss();             }              if (success) {                 Toast.makeText(context, "OK", Toast.LENGTH_LONG).show();             } else {                 Toast.makeText(context, "Error", Toast.LENGTH_LONG).show();             }         }          protected Boolean doInBackground(final String... args) {             try{                     BaseFeedParser parser = new BaseFeedParser();                 messages = parser.parse();                   return true;              } catch (Exception e){                 Log.e("tag", "error", e);                 return false;              }           }       }  } 

@Vladimir, thx your code was very helpful.

like image 26
Houssem Avatar answered Sep 20 '22 05:09

Houssem