0

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>
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193

1 Answers1

0

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?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thanks a lot for your kind response. Science I'm a beginner of this filed can you please tell me where should include this line. In my activity or adapter class and under which method as well – Sawon ahmed Apr 20 '22 at 08:23
  • `onItemSelected` is a method within the spinner adapter. So that's the place where you should add that logic. – Alex Mamo Apr 20 '22 at 08:28
  • Hey Sawon. Can I help you with other information? – Alex Mamo Apr 21 '22 at 06:23
  • Oh Thanks -Alex Momo. In previous I was trying to do the sorting with spinner menu and I was confused how to do that then I create a spinner in the activity and now according to your method it's working. Can you please help me to another fact ? If I want to sort the view with 2 spinner then how can I – Sawon ahmed Apr 21 '22 at 10:40
  • Good to hear that you made it work. Regarding the second question, without seeing what you have already tried, it's hard to help. So please post a new question, here on StackOverflow, using its own [MCVE](https://stackoverflow.com/help/mcve), so I and other Firebase developers can help you. – Alex Mamo Apr 21 '22 at 10:52
  • Yes sure if you wanna share more information I don't mind – Sawon ahmed Apr 21 '22 at 18:38