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;
}
}