How to use AsyncTask to display a ProgressBar and load Rss

I am using AsyncTask to display a progressBar when loading Rss News. Since this is my first time using asyncTask, I donโ€™t know if my method is suitable, so please tell me if this is good for you? Its work, but I want to just be a day! Thanks.

public class BackgroundAsyncTask_nea extends AsyncTask<Void, Void, Void> { private ProgressDialog dialog; int myProgress; @Override protected void onPostExecute(Void result) { // TODO Auto-generated method stub displayRss(); dialog.dismiss(); } @Override protected void onPreExecute() { // TODO Auto-generated method stub dialog = ProgressDialog.show(nea.this, "", "Loading. Please wait...", true); myProgress = 0; } @Override protected void onProgressUpdate(Void... values) { // TODO Auto-generated method stub //super.onProgressUpdate(values); } @Override protected Void doInBackground(Void... arg0) { // TODO Auto-generated method stub loadFeed(); return null; } } 

loadFeed ();

 private void loadFeed(){ try{ BaseFeedParser parser = new BaseFeedParser(); messages = parser.parse(); } catch (Throwable t){ Log.e("OSFP.News",t.getMessage(),t); finish(); } } 

displayRss ();

 private void displayRss(){ ArrayList<HashMap<String, String>> List_nea = new ArrayList<HashMap<String, String>>(messages.size()); for (Message msg : messages){ des.add(msg.getDescription());// keimeno text.add(msg.getTitle());// titlos url.add(msg.getLink());// link imgl.add(msg.getImgLink()); HashMap<String, String> map = new HashMap<String, String>(); map.put("name", msg.getTitle()); map.put("date", msg.getDate()); List_nea.add(map); ListAdapter mSchedule = new SimpleAdapter(this, List_nea, R.layout.row, new String[] {"name", "date"}, new int[] {R.id.TextView01, R.id.TextView02}); this.setListAdapter(mSchedule); } AnimationSet set = new AnimationSet(true); Animation animation = new AlphaAnimation(0.0f, 1.0f); animation.setDuration(400); set.addAnimation(animation); animation = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f ); animation.setDuration(400); set.addAnimation(animation); LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f); ListView listViewn = getListView(); listViewn.setLayoutAnimation(controller); } 
+4
source share
3 answers

To update the progress bar, you need to make calls to the publishProgress () function. You do not do this anywhere in your code. If I were you, I would pass the AsyncTask link to your loadFeed function. Something like that:

  protected Void doInBackground(Void... arg0) { // TODO Auto-generated method stub loadFeed(this); return null; } private void loadFeed(AsyncTask<Void, Void, Void> asyncTask){ // make calls to publishProgress in here. try{ BaseFeedParser parser = new BaseFeedParser(); messages = parser.parse(); } catch (Throwable t){ Log.e("OSFP.News",t.getMessage(),t); finish(); } } 

You will also need to implement the onProgressUpdate() method, which you do not have. Here you will actually update the value of your progress bar.

  protected void onProgressUpdate(Integer... progress) { dialog.setProgress(progress[0]); } 
+2
source

Yes. This is the right workflow, showing the run dialog before executing in the user interface thread (it might have been done outside of this class, but it looks better), doing work on the doInBackground method, updating ProgressDialog with publishProgress (), if necessary, then rejecting your ProgressDialog and showing the results of onPostExecute () in the user interface thread.

0
source

Inside loadFeed parsing, you can use the AsyncTask link with the publishProgress method to send progress updates to onProgressUpdate , where you can update the progress bar.

0
source

Source: https://habr.com/ru/post/1383446/


All Articles