'Filtering RecyclerView with spinner menu selected item

My RecyclerView is working properly with one model class and custom adapter since my data comes from the Firebase Realtime Database. But now I would like to filter the adapter with a spinner menu. Like when the user selects an item from the menu the RecyclerView will be filtered and only it will show the item which is similar to the selected item. So how can I implement this?

public class Doctor extends AppCompatActivity {
    RecyclerView recyclerView2;
    myAdapter2 adapter2;
    @SuppressLint("ResourceType")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_doctor);
        recyclerView2=findViewById(R.id.recylerView2);
        recyclerView2.setLayoutManager(new LinearLayoutManager(this));
        FirebaseRecyclerOptions<model2> options =
                new FirebaseRecyclerOptions.Builder<model2>()
                        .setQuery(FirebaseDatabase.getInstance().getReference().child("Doctor_list"), model2.class)
                        .build();
        adapter2=new myAdapter2(options);
        recyclerView2.setAdapter(adapter2);
    }
    @Override
    protected void onStart() {
        super.onStart();
        adapter2.startListening();
    }
    @Override
    protected void onStop() {
        super.onStop();
        adapter2.stopListening();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.spinnermenu,menu);
        return super.onCreateOptionsMenu(menu);
    }
}

Here is my custom menu

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/medicine"
        android:title="Medicine"
        app:showAsAction="never">
    </item>
    <item
        android:id="@+id/phycology"
        android:title="Phycology"
        app:showAsAction="never">
    </item>
    <item
        android:id="@+id/orthopedic"
        android:title="Orthopedic"
        app:showAsAction="never">
    </item>
</menu>


Solution 1:[1]

Assuming that you already have implemented a spinner, to create a new query when the user selects one of the options within the spinner, you'll have to update the adapter to use that new query. To achieve that, you have to create a new FirebaseRecyclerOptions object with the new query and then set that on the adapter by calling its updateOptions() method.

Assuming that you have two different values in the spinner at position zero and position one, please use the following lines of code:

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    if (position == 0 ){
        query = db.child("Doctor_list");
    } else if (position == 1) {
        query = reference.child("Doctor_list").orderByChild("name").equalTo("Sawon ahmed");
    }
    FirebaseRecyclerOptions<model2> newOptions = new FirebaseRecyclerOptions.Builder<model2>()
            .setQuery(query, model2.class)
            .build();
    adapter2.updateOptions(newOptions); ? //Change the options object in the adapter.
}

Where the adapter2 object is the original adapter object that you have passed to the adapter.

P.S.

If you're reading that data for the spinner, also from the Realtime Database, please check this answer:

How to get data from Firebase Realtime Database and show it in a spinner?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alex Mamo