我有一个 ArrayAdapter
,里面有一个 AsyncTask
,但我不确定如何从 onPostExecute< 调用 notifyDataSetChanged
/
例子:
public class ColorAdapter extends ArrayAdapter<Color> {
List<Color> colorList;
Context context;
....
public ColorAdapter(Context context, List<Color> list) {
this.context = context; this.colorList = list;
}
public View getView (final int position, final View view, final ViewGroup parent) {
.....
}
class DeleteColorTask extends AsyncTask <String, String, String> {
int colorId;
DeleteColorTask (int colorId) {this.colorId = colorId;}
protected String doInBackgroud (String ... args) {
//call to server to delete the color
colorList.remove(colorList.indexOf(...));
}
protected void onPostExecute(String s) {
//HOW CAN I CALL notifyDataSetChanged() here?? this.notifyDataSetChanged() doesn't work since I am inside DeleteColorTask class
}
}
}
我从我的 Activity 中这样调用上面的内容:
adapter = new ColorAdapter(context, colorsList);
setListAdapter(adapter);
你可以这样调用它:
ColorAdapter.this.notifyDataSetChanged();
顺便说一句,启动此 AsyncTask
的更合适的位置是它的主机 fragment/Activity ,为什么?AsyncTasks 有时会比您预期的停留时间更长,如果您没有适本地管理它们的生命周期,它们可能会造成麻烦。
我是一名优秀的程序员,十分优秀!