无法按升序检索数据

时间:2018-04-14 13:39:28

标签: android firebase firebase-realtime-database firebaseui

我想按升序检索数据,但收到的数据以某种方式按降序添加到recyclerview。

Firebase数据库: database structure

我在做什么:

    DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference().child("/Teacher");

    Query query = mDatabase.orderByChild("rating");

    FirebaseRecyclerOptions<Teacher> options =
            new FirebaseRecyclerOptions.Builder<Teacher>()
                    .setQuery(query, Teacher.class)
                    .build();

    FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Teacher, TeacherViewHolder>(options) {
        @NonNull
        @Override
        public TeacherViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.teacher_list_item, parent, false);

            return new TeacherViewHolder(view);

        }

        @Override
        protected void onBindViewHolder(@NonNull final TeacherViewHolder holder, int position, @NonNull final Teacher model) {

           // some stuff here

        }
    };

2 个答案:

答案 0 :(得分:0)

由于您无法使用RTDB本地执行此操作,因此您可能希望执行以下操作:

LinearLayoutManager layoutManager = new LinearLayoutManager(context);
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager);

有关详细信息,请参阅https://stackoverflow.com/a/27727398/4548500

答案 1 :(得分:0)

要获得已排序和过滤的结果,您必须将查询传递到FirebaseRecyclerAdapter构造函数中:

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

然后使用订单和过滤器的查询调用它:

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