android - AsyncTask 中的 ProgressDialog

标签 android android-asynctask progressdialog

我试图在从 HTTP 服务器加载 RSS 提要时显示自定义进度对话框,我进行了艰苦的搜索,但没有任何帮助我做到这一点,我唯一知道的是解决方案应该使用 AsyncTask ,但我对传递给此 AsyncTask 的参数感到困惑。 这是我的 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);
        }
    }

}

你能帮我把 AsyncTask 添加到这个吗?

最佳答案

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

}

关于android - AsyncTask 中的 ProgressDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4538338/

相关文章:

android - 滚动屏幕时清晰显示视频 View ?

android - 为什么 Android API 级别 9 已过时?

android - 异步任务异常

java - 通过应用程序显示 ProgressDialogs 的最佳实践?

java - progressDialog 在 fragment 打开之前被关闭

android - 如何在初始化 recyclerview 适配器时保持 UI 解锁?

java - Android - 设置应用程序主题(样式)

java - 在 AsyncTask 中设置循环以检索序列的下一次迭代

android - 异步任务大量使用

android - 更改 Android 进度对话框背景颜色