ProgressDialog in AsyncTask

后端 未结 7 2339
一个人的身影
一个人的身影 2020-11-22 07:54

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 t

7条回答
  •  春和景丽
    2020-11-22 08:36

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

    public class Soirees extends ListActivity {
        private List 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 {
            private ProgressDialog dialog;
            List titles;
            private ListActivity activity;
            //private List 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 titles = new ArrayList(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.

提交回复
热议问题