在过滤RecyclerView中的特定项目时出现问题

时间:2020-02-23 09:48:32

标签: android android-recyclerview

我是android的新手。我正在构建一个包含NoticesFragment的应用程序。在该片段内部,我有一个可容纳一些数据的recyclerview。为了过滤RecyclerView中存在的数据,我需要两个CheckBox(COLLEGE和BRANCH)的帮助,如果我单击COLLEGE则保存()值的项目NoticeBy:COLLEGE)必须被过滤并显示在recyclerview上。 同样,如果我单击“分支”,则必须显示相关项目。

这是我的模特班:

public class NoticeContents
{
    private String noticeHeading;
    private String noticeContent;
    private String Date;
    private String noticeBy;
    private String forYears;
    private String forBranches;

    public NoticeContents(String noticeHeading, String noticeContent, String date, String noticeBy, String forYears, String forBranches) {
        this.noticeHeading = noticeHeading;
        this.noticeContent = noticeContent;
        Date = date;
        this.noticeBy = noticeBy;
        this.forYears = forYears;
        this.forBranches = forBranches;
    }

    public String getNoticeHeading() {
        return noticeHeading;
    }

    public void setNoticeHeading(String noticeHeading) {
        this.noticeHeading = noticeHeading;
    }

    public String getNoticeContent() {
        return noticeContent;
    }

    public void setNoticeContent(String noticeContent) {
        this.noticeContent = noticeContent;
    }

    public String getDate() {
        return Date;
    }

    public void setDate(String date) {
        Date = date;
    }

    public String getNoticeBy() {
        return noticeBy;
    }

    public void setNoticeBy(String noticeBy) {
        this.noticeBy = noticeBy;
    }

    public String getForYears() {
        return forYears;
    }

    public void setForYears(String forYears) {
        this.forYears = forYears;
    }

    public String getForBranches() {
        return forBranches;
    }

    public void setForBranches(String forBranches) {
        this.forBranches = forBranches;
    }
}

还有我的Adapterclass:

    public class NoticeAdapter extends RecyclerView.Adapter<NoticeAdapter.ViewHolder>
{
    public ArrayList<NoticeContents> notices;
    public ArrayList<NoticeContents> copyOfNotices;
    public static Context context;


    public NoticeAdapter(Context context, ArrayList<NoticeContents> list)
    {
        notices = list;
        this.context = context;
        copyOfNotices = list;
    }

    public void filter(String type) {
        ArrayList<NoticeContents> filteredList = new ArrayList<>();
        if(!filteredList.isEmpty()){
            filteredList.clear();
        }

        type = type.toLowerCase();

        if (type.length() == 0) {
            copyOfNotices = notices;
        } else {


            for (NoticeContents wp : copyOfNotices) {
                if (wp.getNoticeBy().toLowerCase().equals(type)) {
                    filteredList.add(wp);
                }
            }
            copyOfNotices = filteredList;

        }

        notifyDataSetChanged();
    }



    @NonNull
    @Override
    public NoticeAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, final int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.notices_layout,viewGroup,false);
        context = viewGroup.getContext();

        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull final NoticeAdapter.ViewHolder viewHolder, final int i) {

        bindData(viewHolder,i);

    }

    private void bindData(NoticeAdapter.ViewHolder viewHolder, int i) {

        //getting the data from ArrayList and sending that through the following methods
        String heading = notices.get(i).getNoticeHeading();
        String content = notices.get(i).getNoticeContent();
        String date = notices.get(i).getDate();
        String noticeBy = notices.get(i).getNoticeBy();
        String forYears = notices.get(i).getForYears();
        String forBranches = notices.get(i).getForBranches();

        viewHolder.notice_heading.setText(heading);
        viewHolder.notice_content.setText(content);
        viewHolder.notice_by_whom.setText(noticeBy);
        viewHolder.notice_date.setText(date);
        viewHolder.tvYears.setText(forYears);
        viewHolder.tvBranches.setText(forBranches);


    }


    @Override
    public int getItemCount() {
        return copyOfNotices.size();
    }


    public static class ViewHolder extends RecyclerView.ViewHolder
    {
        private static TextView notice_heading;
        private static TextView notice_content;
        private static TextView notice_by_whom;
        private static TextView notice_date;
        private static TextView tvYears;
        private static TextView tvBranches;


        public ViewHolder(@NonNull final View itemView) {
            super(itemView);
            notice_heading = itemView.findViewById(R.id.notice_heading);
            notice_content = itemView.findViewById(R.id.notice_content);
            notice_by_whom = itemView.findViewById(R.id.notice_by_whom);
            notice_date = itemView.findViewById(R.id.tv_notice_Date);
            tvYears = itemView.findViewById(R.id.tvYears);
            tvBranches = itemView.findViewById(R.id.tvBranches);

        }

    }

}

在Notices片段中,我正在这样调用filter方法:

chk_College.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            chk_College.setChecked(true);
            chk_Branch.setChecked(false);
            chk_Branch.setTextColor(getResources().getColor(R.color.colorWhite));
            chk_College.setTextColor(getResources().getColor(R.color.colorAccent));

            myAdapter.filter("COLLEGE");

        }
    });

    chk_Branch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            chk_Branch.setChecked(true);
            chk_College.setChecked(false);
            chk_College.setTextColor(getResources().getColor(R.color.colorWhite));
            chk_Branch.setTextColor(getResources().getColor(R.color.colorAccent));


            myAdapter.filter("BRANCH");
        }
    });

0 个答案:

没有答案
相关问题