progressDialog在AsyncTask中

我试图显示一个自定义progressdialog加载rss饲料从http服务器,我做了一个艰难的search,但没有什么帮助我做到这一点,我知道的唯一的解决scheme应该使用AsyncTask,但我很困惑params传递给这个AsyncTask。 这是我的活动:

import java.util.ArrayList; import java.util.List; import com.cyberesa.info.BaseFeedParser; import com.cyberesa.info.Message; import com.cyberesa.info.MessageListAdapter; import com.cyberesa.info.R; import android.app.ListActivity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; 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); } } } 

你能帮我添加AsyncTask到这个?

谢谢你,Houssem

 /** * 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; } } 

通过将视图修饰符移动到onPostExecute来修复,所以固定的代码是:

 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你的代码是非常有帮助的。

AsyncTask非常有帮助!

 class QueryBibleDetail extends AsyncTask<Integer, Integer, String>{ private Activity activity; private ProgressDialog dialog; private Context context; public QueryBibleDetail(Activity activity){ this.activity = activity; this.context = activity; this.dialog = new ProgressDialog(activity); this.dialog.setTitle("查询经文"); this.dialog.setMessage("正在查询:"+tome+chapterID+":"+sectionFromID+"-"+sectionToID); if(!this.dialog.isShowing()){ this.dialog.show(); } } @Override protected String doInBackground(Integer... params) { Log.d(TAG,"经文doInBackground"); publishProgress(params[0]); if(sectionFromID > sectionToID){ return ""; } String queryBible = "action=query_bible&article="+chapterID+"&id="+tomeID+"&verse_start="+sectionFromID+"&verse_stop="+sectionToID+""; try{ String bible = (Json.getRequest(HOST+queryBible)).trim(); bible = android.text.Html.fromHtml(bible).toString(); return bible; }catch(Exception e){ e.printStackTrace(); } return null; } @Override protected void onPostExecute(String bible){ Log.d(TAG,"经文onPostExecute"); TextView bibleBox = (TextView) findViewById(R.id.bibleBox); bibleBox.setText(bible); this.dialog.dismiss(); } } 

几天前,我发现了这个问题的一个非常好的解决scheme。 在这里阅读。 用两个词Mike创build了一个调解ProgressDialog和AsyncTask的AsyncTaskManager。 使用这个解决scheme非常简单。 你只需要在你的项目中包含几个接口和几个类,并在你的活动中写一些简单的代码,并从BaseTask嵌套你的新的AsyncTask。 我也build议你阅读评论,因为有一些有用的提示。

这个问题已经过了几年了(因为有人发布了回复)。 此后,根据Android的官方文档 ,ProgressDialog在API级别O中被弃用。 因此,您可以考虑使用内联进度条,而不是文档作者所build议的ProgressDialog。

这个问题已经得到了解答,这里的大多数答案都是正确的,但是它们并没有解决configuration改变的一个主要问题。 看看这篇文章https://androidresearch.wordpress.com/2013/05/10/dealing-with-asynctask-and-screen-orientation/如果你想写一个更好的方式asynchronous任务。;