Я новичок ie до android. Я создаю приложение, в котором у меня есть NoticesFragment. Внутри этого фрагмента у меня есть обзор повторов, который содержит некоторые данные. Чтобы отфильтровать данные, присутствующие внутри RecyclerView, я использую два флажка (COLLEGE и BRANCH), если я нажимаю на COLLEGE элементы, которые содержат значение ( NoticeBy: COLLEGE) должен быть отфильтрован и отображен в обзоре утилизатора. Точно так же, если я нажимаю на ФИЛИАЛ, соответствующие элементы должны отображаться.
Вот мой класс модели:
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;
}
}
И мой класс адаптера:
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);
}
}
}
Внутри фрагмент уведомлений я вызываю метод фильтра следующим образом:
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");
}
});