简体   繁体   English

为什么重置我的适配器不使用新的ArrayList?

[英]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.

相关问题 为什么我的自定义适配器拒绝ArrayList? - Why is my custom adapter rejecting ArrayList? 为什么(新)Admob尝试将我的自定义中介适配器投射到旧版本? - Why does (new) Admob attempt to cast my custom mediation adapter to the old version? 为什么在 LinkedList 末尾添加元素比 ArrayList 花费更长的时间? - Why does adding an element at the end of LinkedList take longer than an ArrayList? 是否有充分的理由为什么ArrayList不将Array用作构造函数参数? - Is there any good reason why ArrayList does not take Array as a constructor argument? 为什么添加到ArrayList中的新元素会覆盖所有先前的值? - Why does the new element added into my ArrayList overwrite all previous values? 当我尝试添加ArrayList元素时,即使它是新的,为什么我的程序仍然存在? - Why does my program say the ArrayList element exists when I try to add it even though it's new? 为什么我的Arraylist继续打印[3,2]? - Why does my Arraylist keep printing [3,2]? 为什么我的recyclerview适配器不起作用? /为什么班级没有上课? - Why does my recyclerview adapter not work? / Why is the class not running? 为什么我的arraylist的一次迭代工作,而另一次没有? - Why does one iteration of my arraylist work, while the other does not? 为什么 new ArrayList&lt;&gt;().add(someObject) 在 lambda 中工作? - Why does new ArrayList<>().add(someObject) work in a lambda?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM