Skip to content
Advertisement

Filtered Recyclerview on spinner with selected class of the student?

I want get items on RecyclerView based on the student class selected in the spinner. The student has rollno,name, class to be displayed in the RecyclerView. For instance, I want students in class 8th. I am using firebase Firestore. Right now I have recyclerviw which fetches all the documents(students).

Updated Code for Adapter is :

public class ClasswiseAdapter extends RecyclerView.Adapter<ClasswiseAdapter.studentViewHolder>{
    private ArrayList<ModelViewClasswise> classlist;
    private ArrayList<ModelViewClasswise> newClassList;



    public ClasswiseAdapter(ArrayList<ModelViewClasswise> classlist) {

        this.classlist = classlist;


    }
    public void addnewList(ArrayList<ModelViewClasswise> newClassList){
        this.classlist = newClassList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ClasswiseAdapter.studentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.row_classwise,parent,false);
        return new ClasswiseAdapter.studentViewHolder(view);


    }

    @Override
    public void onBindViewHolder(@NonNull studentViewHolder holder, int position) {

        holder.t1.setText(classlist.get(position).getName());
        holder.t2.setText(classlist.get(position).getClassName());
        holder.t3.setText(classlist.get(position).getRollNumber());


    }







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


    }





    class studentViewHolder extends RecyclerView.ViewHolder{
        TextView t1,t2,t3;

        public studentViewHolder(@NonNull View itemView) {
            super(itemView);
            t1=itemView.findViewById(R.id.nameC);
            t2=itemView.findViewById(R.id.classC);
            t3=itemView.findViewById(R.id.rollNumC);

        }

    }

}

And updated code for ClassView for students is:

public class ViewClasswise extends AppCompatActivity {

    private static final String TAG = ViewClasswise.class.getSimpleName();

    private RecyclerView recyclerViewCW;
    ArrayList<ModelViewClasswise>classlist;
    FirebaseFirestore db;
    FirebaseAuth fAuth;
    ClasswiseAdapter classwiseAdapter;
    Spinner s1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_classwise);

        recyclerViewCW = findViewById(R.id.recyclerviewCW);
        recyclerViewCW.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));

        recyclerViewCW.setLayoutManager(new LinearLayoutManager(this));
        classlist=new ArrayList<>();
        classwiseAdapter=new ClasswiseAdapter(classlist);
        recyclerViewCW.setAdapter(classwiseAdapter);
        s1=findViewById(R.id.spinnerSearch);

        db= FirebaseFirestore.getInstance();
        fAuth= FirebaseAuth.getInstance();
        FirebaseUser user = fAuth.getCurrentUser();

        //spinner Listener
        s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
            {
                // this returns the class the user selected
                String selectedClass = (String) parentView.getItemAtPosition(position);

                classwiseAdapter.addnewList(listForClass(selectedClass));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView)
            {

            }
            // function to filter the list based on the users choice
            private ArrayList<ModelViewClasswise> listForClass(String selectedClass){
                ArrayList<ModelViewClasswise> newList = new ArrayList<>();

                for(ModelViewClasswise modelClass : classlist){
                    if(modelClass.getClass().equals(selectedClass))
                        newList.add(modelClass);
                }

                return newList;
            }
        });



                    db.collection("Schools").document(user.getUid()).collection("Students").get()
                            .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                                @Override
                                public void onSuccess(QuerySnapshot queryDocumentSnapshots) {

                                    List<DocumentSnapshot> list=queryDocumentSnapshots.getDocuments();

                                    for(DocumentSnapshot d:list){

                                        ModelViewClasswise obj=d.toObject(ModelViewClasswise.class);
                                        classlist.add(obj);
                                    }
                                    //Update Adapter

                                    classwiseAdapter.notifyDataSetChanged();
                                }
                            });


    }


}

Advertisement

Answer

Start with adding a function addNewList in your adapter class to update the classlist based on the user’s choice

Adapter class

 public void addnewList(ArrayList<ModelViewClasswise> newClassList){
     this.classlist = newClassList;
     notifyDataSetChanged()
 }

Main activity

// assuming you have initialized the spinner with the data to show

yourSpinner.setOnItemSelectedListener(new OnItemSelectedListener() 
{
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) 
    {
        // this returns the class the user selected
        String selectedClass = parentView.getItemAtPosition(position);

        classwiseAdapter.addnewList(listForClass(selectedClass))
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) 
    {
       
    }
});


// function to filter the list based on the users choice 
 private ArrayList<ModelViewClasswise> listForClass(String selectedClass){
    ArrayList<ModelViewClasswise> newList = new ArrayList<>();
    
    for(ModelViewClasswise modelClass : classlist){
           if(modelClass.getClass().equals(selectedClass))
                 newList.add(modelClass);
    }

   return newList;
 }

Edit:

// add this after fetching all the objects from the Firebase Firestore
classwiseAdapter.addnewList(classlist);
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement