【发布时间】: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