gpt4 book ai didi

java - Android:正确更新 BaseAdapter 的 ArrayList?

转载 作者:行者123 更新时间:2023-12-01 13:24:22 26 4
gpt4 key购买 nike

我看过很多示例/教程,它们解释了如何更新 BaseAdapter 使用的 ArrayList。例如:

public class CustomAdapter extends BaseAdapter {
private ArrayList<Item> listData;
private LayoutInflater layoutInflater;

public CustomListAdapter(Context context, ArrayList<Item> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
}

//called to update the ListView
public void resetList(ArrayList<Item> newList) {
this.listData = newList;
this.notifyDataSetChanged();
}

}

另一种方法是清除并填充 listData 当前指向的原始 ArrayList,并从 Activity 的线程调用 notifyDataSetChanged()

为什么这是经常推荐的方式?如果我在修改 ArrayList 之后或同时、但在调用 notificationDataSetChanged() 之前直接单击 ListView 项,会发生什么情况?

最佳答案

由于您是从 Activity 的 (UI) 线程调用 resetList(),因此您无法单击方法执行中间的项目,因为单击是由同一 UI 线程处理的,因此,您只能在方法执行之前或之后执行单击。

关于java - Android:正确更新 BaseAdapter 的 ArrayList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21850673/

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