我有一个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
}
}
}我从我的活动中得出如下结论:
adapter = new ColorAdapter(context, colorsList);
setListAdapter(adapter);发布于 2014-02-21 22:42:47
你可以这样称呼它:
ColorAdapter.this.notifyDataSetChanged();顺便说一句,启动这个AsyncTask更合适的地方是它的主机片段/活动,为什么?AsyncTasks有时会比您预期的时间更长,如果您不适当地管理它们的生命周期,它们可能会带来麻烦。
https://stackoverflow.com/questions/21946604
复制相似问题