how to filter what will appear in Recyclerview

Asked

Viewed 28 times

-1

I want to filter what will appear in Recyclerview, is possible?

There is a Calendarview and I want that when someone clicks on a day from the calendarview, it shows itemviews only from that date, for example, when someone clicks on the 15th of the month can, it shows only the tasks of that day

I don’t know if it’s possible. If anyone can help me, it would be great.

package com.iza.letfly;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Paint;
import android.os.Build;
import android.preference.PreferenceManager;
import android.text.Html;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class AdapterDoes extends RecyclerView.Adapter<AdapterDoes.MyViewHolder> {

    Context context;
    ArrayList<Mydoes> mydoes;
    FirebaseFirestore firestore;
    FirebaseUser user;


    public  AdapterDoes(Context c, ArrayList<Mydoes> p){
       context = c;
       mydoes = p;

    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup ViewGroup, int i) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_does, ViewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
        myViewHolder.titleDoes.setText(mydoes.get(i).getTitleDoes());
        myViewHolder.DescribeDoes.setText(mydoes.get(i).getDescribeDoes());
        myViewHolder.dateDoes.setText(mydoes.get(i).getDateDoes());
        myViewHolder.alarm.setText(mydoes.get(i).getAlarm());
        myViewHolder.checkBox2.setText(mydoes.get(i).getCheck());

        myViewHolder.checkBox2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                    if (myViewHolder.checkBox2.isChecked()){
                        myViewHolder.checkBox2.setChecked(false);
                    }else{
                        myViewHolder.checkBox2.setChecked(true);
                    }

                }
        });

        if(myViewHolder.checkBox2.getText().equals("CHECKED")){
            myViewHolder.checkBox2.setChecked(true);
                myViewHolder.titleDoes.setAlpha(0.4f);
                myViewHolder.DescribeDoes.setAlpha(0.5f);
            myViewHolder.DescribeDoes.setPaintFlags(myViewHolder.DescribeDoes.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            myViewHolder.titleDoes.setPaintFlags(myViewHolder.titleDoes.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        }


        if(myViewHolder.checkBox2.getText().equals("UNCHECKED")){
            myViewHolder.checkBox2.setChecked(false);
        }


        myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {


            final String getTitleDoes = mydoes.get(i).getTitleDoes();
            final String getDescribeDoes = mydoes.get(i).getDescribeDoes();
            final String getDateDoes = mydoes.get(i).getDateDoes();
            final String getAlarm = mydoes.get(i).getAlarm();
            final String getKeyDoes = mydoes.get(i).getKeyDoes();
            final String getCheck = mydoes.get(i).getCheck();


            @Override
            public void onClick(View v) {
                Intent i = new Intent(context,Edit_Check.class);
                i.putExtra("titleDoes",getTitleDoes);
                i.putExtra("DescribeDoes",getDescribeDoes);
                i.putExtra("dateDoes",getDateDoes);
                i.putExtra("alarm",getAlarm);
                i.putExtra("keyDoes",getKeyDoes);
                i.putExtra("check", getCheck);
                context.startActivity(i);
            }
        });



    }

    @Override
    public int getItemCount() {

        return mydoes.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView titleDoes,DescribeDoes,dateDoes, alarm, keyDoes;
        CheckBox checkBox2;


        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            titleDoes = (TextView) itemView.findViewById(R.id.titleDoes);
            DescribeDoes = (TextView) itemView.findViewById(R.id.DescribeDoes);
            dateDoes = (TextView) itemView.findViewById(R.id.dateDoes);
            alarm = (TextView) itemView.findViewById(R.id.alarm);
            checkBox2 = (CheckBox) itemView.findViewById(R.id.checkBox2);



        }


    }
}

1 answer

0

Recyclerview will show the data that it has, in case of your code, it will show the data in mydoes. If you want to filter the result in Recyclerview, the best way is to filter it externally (e.g. in your Viewmodel/Fragment/Activity when the person selects a date) and pass the new filtered data to Recyclerview. You can do this by adding a new method that gets the new data:

public void setData(ArrayList<Mydoes> newMydoes) {
    mydoes = newMydoes;
    notifyDataSetChanged();
}

Don’t forget that call to notifyDataSetChanged() to warn Recyclerview that the data has been updated, if not by changing the mydoes, Recyclerview does not update visually. In practice, when calling this method, it will recall your getItemCount() to see the new amount of data, and call a onBindViewHolder() (and possibly onCreateViewHolder()) for each element of the new mydoes to update the views correctly. Actually, I suggest using the class androidx.recyclerview.widget.DiffUtil instead of calling the method notifyDataSetChanged() to notify that your data has changed, because the DiffUtil runs faster and still animates between the elements during this change.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.