首页 > 解决方案 > 我需要在哪里选择要在 RecylerView 中显示的?

问题描述

可能这个问题并不像我希望的那样容易理解。

我创建了一个 RecylerView,它包含c_TakeTimeObjects来自 ArrayList 的类型的对象。这些对象具有关于它们自身的特殊信息,例如日期。

因此,ArrayList 中可能有 20 个这些对象,而我想要做的只是显示“日期”值与 RecylerView 上方的 TextView 中显示的日期值匹配的对象。

如果 TextView 显示“12.07.2016”,则仅应显示 ArrayList 中“日期”值设置为“12.07.2016”的元素。如果没有任何具有这些“日期”值的对象,则列表应该为空。

为了尝试这一点,我做了以下事情:

 @Override
    public void onBindViewHolder(@NonNull c_ViewHolder c_viewHolder, int i) {
        c_TakeTimeObjects currentItem = c_takeTimeObjects.get(i);
        if(currentItem.getiActivityDate().equals("12.06.2016")) {
            c_viewHolder.mImageView.setImageResource(currentItem.getiImageResource());
            c_viewHolder.mCardview.setLayoutParams(new CardView.LayoutParams((int) (currentItem.getiActivityTime() * fDISPLAYFACTOR), CardView.LayoutParams.WRAP_CONTENT));
            c_viewHolder.mCardview.setCardBackgroundColor(currentItem.getiImageColor());
        }
    }

这是 ArrayList 的内容:


 private void createExampleList() {
        StatusImageList = new ArrayList<>();
        StatusImageList.add(new c_TakeTimeObjects(c_GlobalValues.iBreakMode,((int) (idefaultRestMinutesEarly)), "12.07.2016",this));
        StatusImageList.add(new c_TakeTimeObjects(c_GlobalValues.iWorkingMode, ((int) (idefaultWorkingMinutes)), "12.07.2016",this));
        StatusImageList.add(new c_TakeTimeObjects(c_GlobalValues.iBreakMode, ((int) (idefaultBreakMinutes)), "12.07.2016",this));
        StatusImageList.add(new c_TakeTimeObjects(c_GlobalValues.iWorkingMode, ((int) (idefaultWorkingMinutes)),"12.07.2016",this));
        StatusImageList.add(new c_TakeTimeObjects(c_GlobalValues.iBreakMode, ((int) (idefaultRestMinutesLate)),"12.07.2016",this));
    }

请参阅“12.06.2019”到“12.07.2019”的差异。

我以为 RecylerView 现在是空的,但实际上它添加了 5 个没有任何内容的白色元素。我是否以错误的方法要求日期或我哪里出错了?

标签: androidandroid-recyclerview

解决方案


RecyclerView 适配器没有过滤数据的概念。这是你需要自己处理的事情。您可以做的一件事是在过滤列表后将您的 RecyclerView 适配器传递给您的列表。

例如:

List<c_TakeTimeObjects> statusImageList = createExampleList();
Iterator<c_TakeTimeObjects> iter = statusImageLists.iterator();
while (iter.hasNext()) {
   if (iter.next().getActivityDate() != /* ... */) {
       iter.remove()
   }
}

// Set data, constructor, or whatever method you use to set list data
adapter.setData(statusImageList);

适配器非常擅长一件事:为列表中的项目呈现视图。否则不包括过滤或转换。


推荐阅读