0
我使用ArrayAdapter在ListView中显示项目。此ListView中的每一行都拥有一个按钮。在适配器内的AsyncTask期间使用自定义ProgressDialog
只要用户点击其中一个按钮,我就启动一个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 – Rasel
如果:D阱,如果你看到你的适配器的构造
你这么适配器的侧面使用上下文传递上下文它崩溃了,你得到的错误是什么? –