单击按钮后对Firebase Recyclerview进行排序

时间:2019-05-24 13:08:43

标签: java android firebase-realtime-database android-recyclerview firebaseui

我有一个包含数据的recyclerview,数据一开始是随机的,然后我要根据用户单击的按钮对数据进行排序。我正在使用orderByChild()方法,但我不知道如何使用排序后的数据刷新recyclerview。

我尝试使用notifyDataChanged方法,但是没有用。我还尝试创建一个新的适配器,然后将其传递给recyclerview,但是它将所有的recyclerview删除,从而使该活动为空。

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

0 个答案:

没有答案