0

I have a recycler view with drink categories (7 items), and I wanted all 7 items to change position dynamically based on customer's favorite drink saved in Favorites table.

How could this be possible in such a way?

Drink categories table

Favorites table

My current code:

    private void loadMenu() {

    Query query = category.orderByChild("views");

    adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>(
            Category.class,
            R.layout.menu_item,
            MenuViewHolder.class,
            query
    ) {
        @Override
        protected void populateViewHolder(final MenuViewHolder viewHolder, final Category model, int position) {
            viewHolder.txtMenuName.setText(model.getName());
            Picasso.with(getBaseContext()).load(model.getImage())
                    .into(viewHolder.imageView);
            viewHolder.txtViews.setText(model.getViews());

            viewHolder.setItemClickListener(new ItemClickListener() {
                @Override
                public void onClick(View view, int position, boolean isLongClick) {
                    //No of click
                    count++;
                    category.child(adapter.getRef(position).getKey()).child("views").setValue(count);

                    //Get CategoryId and send to new Activity
                    Intent drinkList = new Intent(Home.this, DrinkList.class);
                    //Because CategoryId is key, so we just get key of this item
                    drinkList.putExtra("CategoryId",adapter.getRef(position).getKey());
                    startActivity(drinkList);
                }
            });
        }
    };
    adapter.notifyDataSetChanged();
    recycler_menu.setAdapter(adapter);
}
Syahirah Isa
  • 9
  • 1
  • 6

1 Answers1

0

If I got you right you want to order the categories based on field like the is favorite or how much the user like the category, if so To get a sorted and filtered result, you have to pass a query into the FirebaseRecyclerAdapter constructor, where you are now passing in a DatabaseReference. So add a constructor to your adapter that takes a Query:

public MyFireAdapterDiasFiestaRecyclerView(
    Class<DiaFiestaMeta> modelClass, 
    int modelLayout, 
    Class<MyFireViewHolder> viewHolderClass, 
    Query query) {
    ...

And then invoke it with a query that orders and filters:

adapter = new MyFireAdapterDiasFiestaRecyclerView(
   DiaFiestaMeta.class,
   layout_that_you_use,
   MyFireViewHolder.class,
   ref.orderByChild("property_to_sort_and_filter_on").equalTo("value_to_filter_on")
);

And orderByChild() will sort your query in ascending order, to have Descending query when using orderByChild() use this trick good solution if you're using the DatabaseReference to populate a FirebaseRecyclerAdapter, here it goes:

Inside the FirebaseRecyclerAdapter declaration, override the getItem() function just like we this :

@Override
public User getItem(int position) {
        return super.getItem(getItemCount() - 1 - position);
}

ref : Sort a FirebaseRecyclerAdapter

tamtom
  • 2,474
  • 1
  • 20
  • 33
  • Thank you for the prompt reply, i will try to this right away, will get back to you soon – Syahirah Isa Dec 09 '17 at 19:43
  • @SyahirahIsa if you had any problems with queries this should help you! https://firebase.google.com/docs/reference/android/com/google/firebase/database/Query – tamtom Dec 09 '17 at 20:12
  • I have added a new child in Category which is "views" equal to int count whenever item in recycler view being clicked. I wanted to sort items in descending order. Here is my query: Query query = category.orderByChild("views"); But, it sort my item in ascending order of value of views – Syahirah Isa Dec 09 '17 at 22:32
  • @SyahirahIsa I have updated the answer to have the items in descending order. – tamtom Dec 10 '17 at 04:38
  • Dear sir, im quite new to coding around for the past 2 months. What do you mean by FirebaseRecyclerAdapter declaration? How do i do it? I've also updated my above code – Syahirah Isa Dec 10 '17 at 08:48
  • @SyahirahIsa can you edit the implementation code for `FirebaseRecyclerAdapter` ? if not this means you need to create your Own `FirebaseRecyclerAdapter` how to do that? create a class call what you want `MyOwnFirebaseAdapter` and make extends the `FirebaseRecyclerAdapter` and Override the `getItem(int position)` method that mentioned in the answer. if you things are not clear to you I will update my answer with an example as soon as possible. – tamtom Dec 10 '17 at 09:23