gpt4 book ai didi

java - 如何从 AsyncTask 中调用 notifyDataSetChanged()

转载 作者:太空宇宙 更新时间:2023-11-03 13:24:54 25 4
gpt4 key购买 nike

我有一个 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 有时会比您预期的停留时间更长,如果您没有适本地管理它们的生命周期,它们可能会造成麻烦。

关于java - 如何从 AsyncTask 中调用 notifyDataSetChanged(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21946604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com