[英]Why does resetting my Adapter not take a new ArrayList?
i am trying to implement an option to hide certain items from an ArrayList "mTaskList". 我试图实现一个选项,以从ArrayList“ mTaskList”隐藏某些项目。
For that, i provide a boolean "hideDues" in the Adapter's Constructor. 为此,我在适配器的构造函数中提供了一个布尔值“ hideDues”。 If it is true, i filter these items out of the list.
如果是真的,我将这些项目从列表中过滤掉。
public DeadlinesAdapter(Context context, ArrayList<TaskItem> taskList, DeadlinesAdapterListener listener, boolean hideDues) {
if (hideDues) {
for (int i = 0; i < taskList.size(); i++) {
if (taskList.get(i).getTimeLeftInMinutes() < 1) taskList.remove(i);
}
}
mTaskList = taskList;
mContext = context;
mListener = listener;
}
It works, but when i set that boolean to false and reset the adapter, it still uses the filtered list, even though the original ArrayList i provide in the Constructor, is unchanged. 它可以工作,但是当我将该布尔值设置为false并重置适配器时,即使我在构造函数中提供的原始ArrayList不变,它仍然使用过滤后的列表。
if (mHideDues) {
mHideDues = false;
item.setIcon(R.drawable.ic_invisible_white);
} else {
mHideDues = true;
item.setIcon(R.drawable.ic_visible_white);
}
mDeadlinesAdapter = new DeadlinesAdapter(this, mTaskList, this, mHideDues);
mDeadlinesRecyclerView.setAdapter(mDeadlinesAdapter);
I change the boolean and reset the Adapter. 我更改了布尔值并重置了适配器。 mTaskList shouldnt have any changes.
mTaskList不应有任何更改。 So why doesnt it take a new ArrayList?
那么,为什么不采用新的ArrayList?
You have to copy your ArrayList
like this for example: 例如,您必须像这样复制
ArrayList
:
ArrayList newList = new ArrayList(oldList);
And only then pass it to the DeadlinesAdapter
. 然后将其传递给
DeadlinesAdapter
。 It should solve your problem. 它应该可以解决您的问题。
Sergei pointed to the problem: You are passing the list of tasks to your adapter, where you filter the list. Sergei指出了问题:您正在将任务列表传递给适配器,在此您可以对列表进行过滤。 Now what you probably want to do is filter a copy of the list.
现在,您可能想要做的是过滤列表的副本。 What you actually do is remove the items from the original list.
您实际要做的是从原始列表中删除项目。 That's why when you set mHideDues to false, nothing happens.
这就是为什么将mHideDues设置为false时,什么都不会发生的原因。
What you can do is simply: 您可以做的只是:
ArrayList<TaskItem> mTaskList = new ArrayList<TaskItem>();
public DeadlinesAdapter(Context context, ArrayList<TaskItem> taskList, DeadlinesAdapterListener listener, boolean hideDues) {
mTaskList.addAll(taskList);
if (hideDues) {
for (int i = 0; i < this.list.size(); i++) {
if (mTaskList.get(i).getTimeLeftInMinutes() < 1) mTaskList.remove(i);
}
}
mContext = context;
mListener = listener;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.