0

I have a recyclerview that contains data, the data was random at first, then I want to sort the data depends on what button the user clicks. I'm using the orderByChild() method, but I don't know how to refresh the recyclerview with the sorted data.

I've tried to use the notifyDataChanged method, didn't work. I also have tried to create a new adapter then pass it to the recyclerview, but it removes all the recyclerview making the activity empty.

@Override
public void onClick(View v) {
    Query query;
    FirebaseRecyclerOptions<RestoranModel> options;
    switch (v.getId()) {
        case R.id.cvSortPrice:
            sortMethod = "minPrice";
            query = FirebaseDatabase.getInstance().getReference("restaurant").orderByChild(sortMethod);
            options = new FirebaseRecyclerOptions.Builder<RestaurantModel>()
                .setQuery(query, RestaurantModel.class)
                .build();

            restaurantAdapter = new RestaurantAdapter(options);
            recyclerView.setAdapter(restaurantAdapter);
            Toast.makeText(MapSheetActivity.this, "Sorted by Price", Toast.LENGTH_SHORT).show();
            break;

        case R.id.cvSortRange:
            sortMethod = "latitude";
            query = FirebaseDatabase.getInstance().getReference("restaurant").orderByChild(sortMethod);
            options = new FirebaseRecyclerOptions.Builder<RestoranModel>()
                .setQuery(query, RestaurantModel.class)
                .build();

            restaurantAdapter = new RestaurantAdapter(options);
            recyclerView.setAdapter(restaurantAdapter);
            Toast.makeText(MapSheetActivity.this, "Sorted by Range", Toast.LENGTH_SHORT).show();
            break;
    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Benaya Rio
  • 125
  • 2
  • 7
  • 1
    Have you tried start listening for changes right after you create a new adapter? – Alex Mamo May 24 '19 at 13:58
  • oh yeah i keep forgetting to start listening for changes. it worked, but now when i clicked on one of the recyclerview it counted as other recyclerview position? – Benaya Rio May 25 '19 at 13:45

0 Answers0