android – Problem while filtering out the particular items inside RecyclerView-ThrowExceptions

Exception or error:

I am a Newbie to android.I am building an application in which i am having a NoticesFragment. Inside that fragment i am having a recyclerview which holds some data.In order to filter the data present inside the RecyclerView i am taking the help of two CheckBoxes (COLLEGE and BRANCH) , if i click on COLLEGE the items that holds the value of (NoticeBy: COLLEGE) has to be filtered and displayed on the recyclerview.
Similarly,if I click on BRANCH the related items has to be displayed.

Here is My Model Class:

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;
    }
}

And my 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);

        }

    }

}

Inside the Notices Fragment i am calling the filter method like this:

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");
        }
    });
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *