在RecycleView Android中使用Checkbox和Filterable

时间:2017-12-28 00:56:41

标签: java android checkbox

我正在创建一个项目,该项目使用Web服务来获取部门数量和名称,并创建动态复选框。 然后我使用filterable过滤适配器,通过检查共享首选项是否选中该复选框如果未选中则尝试将其从适配器中隐藏 (TicketsActivity):

for (int i=0;i<filterList.size();i++) {
    if (!getFromSP(filterList.get(i) + i)) {
       mAdapter.getFilter("Department").filter(filterList.get(i).toLowerCase());
    }
}

和那个

if (recyclerView.getAdapter() == null) {
    mAdapter.notifyItemRangeInserted(0,ticketList.size()-1);
    recyclerView.setAdapter(mAdapter);
} else {
   recyclerView.getAdapter().notifyItemRangeInserted(0,ticketList.size()-1);
}

在我的自定义适配器中 (TicketsAdapter):

public Filter getFilter(final String type) {
    return new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            String charString = charSequence.toString();
            if (charString.isEmpty()) {
                ticketListFiltered = ticketList;
            } else if (type.equals("Search")) {
                List<Ticket> filteredList = new ArrayList<>();
                for (Ticket row : ticketList) {
                    if (row.getTitle().toLowerCase().contains(charSequence) || row.getDate().toLowerCase().contains(charSequence) || row.getPriority().toLowerCase().contains(charSequence)) {
                        filteredList.add(row);
                    }
                }
                ticketListFiltered = filteredList;
            } else {
                List<Ticket> filteredList;
                filteredList = ticketList;
                for (Ticket row : ticketList) {
                    if (row.getDepartment().toLowerCase().contains(charSequence)) {
                        filteredList.remove(row);
                    }
                }
                ticketListFiltered = filteredList;
            }
            FilterResults filterResults = new FilterResults();
            filterResults.values =ticketListFiltered;
            return filterResults;
        }
@Override
public int getItemCount() {
   return  ticketListFiltered == null ? 0 : ticketListFiltered.size();
}

然而,这会在&#39; int java.util.List.size()&#39;上引发异常。 getItemCount,如果我将filteredList.remove(行)更改为filteredList.add(行),它将工作查找,但我需要从&#34;未选中&#34;中删除适配器行。但由于某种原因而不是过滤适配器,它实际上删除了适配器的行。我无法找到我做错的事。 顺便说一句,这适用于SearchView

0 个答案:

没有答案