【问题标题】:Use custom ProgressDialog during AsyncTask within Adapter在适配器内的 AsyncTask 期间使用自定义 ProgressDialog
【发布时间】:2011-08-24 07:24:39
【问题描述】:

我使用 ArrayAdapter 在 ListView 中显示项目。此 ListView 中的每一行都有一个按钮。

每当用户单击其中一个按钮时,我都会启动 AsyncTask 以在后台进行一些处理。

到目前为止,这是有效的。

现在我想在这段时间内显示一个自定义 ProgressDialog。这里让我困惑的是静态便捷方法 ProgressDialog.show() 的第一个参数。在一个活动中,我通常在这里使用“Activityname.this”。但是我应该在适配器中使用什么。我尝试了来自适配器的上下文(崩溃的)、context.getApplicationContext 等等。没有任何效果 - 要么崩溃,要么被编译器拒绝。

所以我今天的问题是:我应该在这个参数中添加什么?

这是我的代码的精简部分:

public class MyAdapter extends ArrayAdapter<MyContainer> {

  private class MyAsyncTask extends AsyncTask<String, Void, Boolean> {

    @Override
    protected void onPreExecute () {
      if (!isRunning) {
        progressDialog = MyProgressDialog.show(?????,
                                               null,
                                               null,
                                               true,
                                               false);
      }
    }

    @Override
    protected Boolean doInBackground(final String... strings) {
      boolean rc = false;

      if (!isRunning) {
        isRunning = true;
        //
        rc = true;
      }

      return rc;
    }

    @Override
    protected void onPostExecute(final Boolean result) {
      if (progressDialog != null) {
        progressDialog.cancel();
      }
      progressDialog = null;

      //    
    }
  }

  private class MyOnClickListener implements OnClickListener {

    private MyContainer container; 

    public MyOnClickListener(final MyContainer container) {
      this.container = container;
    }

    public void onClick(final View view) {
      if (container != null) {
        new MyAsyncTask().execute(container.getUrl());
      }
  }

  private String                 appName = "";
  private ArrayList<MyContainer> containers;
  private Context                context;
  private boolean                isRunning;
  private int                    layout;
  private MyProgressDialog       progressDialog;
  private Resources              resources;

  public MyAdapter(final Context context, final int layout, final ArrayList<MyContainer> containers, final long link_id) {
    super(context, layout, containers);

    this.context = context;
    this.layout = layout;
    this.containers = containers;

    resources = context.getResources();
    appName = resources.getString(R.string.txt_appname);
  }

  @Override
  public View getView(final int position, final View contentView, final ViewGroup viewGroup) {
    //
  }
}

提前致谢。

编辑:在清理项目后使用适配器中的实例变量上下文工作。精氨酸!感谢您的回答。

【问题讨论】:

  • 你尝试过Activityname.this
  • 如果它崩溃了,你得到的错误是什么?

标签: android listview progressdialog android-arrayadapter


【解决方案1】:

嗨 :D 如果你看到你的适配器构造函数

public MyAdapter(final Context context, final int layout, final ArrayList<MyContainer> containers, final long link_id) {
    super(context, layout, containers);

    this.context = context;

您传递上下文,因此在适配器一侧使用上下文 :D 来构建进度对话框

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    • 2017-07-16
    相关资源
    最近更新 更多