如何在Firebase Android中的Recyclerview中使用按值排序?

时间:2019-02-20 22:57:58

标签: android firebase

This is my database.I want to retrieve all the value having in orderbyvalue(CourseValue) that is coming from previous activity

此处CourseName来自上一个活动。问题在于,输出中每次都打印一个额外的回收者视图项。另外,我在其中输入了“ userInfo”的db id数据库引用,以便从回收者视图中获取名称的值。

您能告诉我如何按值顺序检索CourseName的值吗?

    @Override
  public void onResume() {
    super.onResume();


     database = 
FirebaseDatabase.getInstance().getReference("CoursesRegister");


    FirebaseRecyclerOptions<StudentModel> options = new 
    FirebaseRecyclerOptions.Builder<StudentModel>()
                        .setQuery(database, StudentModel.class).build();


    FirebaseRecyclerAdapter adapter = new 
    FirebaseRecyclerAdapter<StudentModel,UserViewHolder>(options) {


        @Override
        protected void onBindViewHolder(final 
      ShowStudentByCourse.UserViewHolder holder, int position, @NonNull 
     final  StudentModel studentModel) {

           final String  selected_user_id = getRef(position).getKey();




       database.child(selected_user_id)
      .orderByValue().equalTo(CourseName).addListenerForSingleValueEvent(new 
       ValueEventListener() {

                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) 
           {


                    if (dataSnapshot.exists()) {


                        Toast.makeText(ShowStudentByCourse.this, "id 
               \n"+dataSnapshot.getKey(), Toast.LENGTH_SHORT).show();






         db.child(dataSnapshot.getKey()).addListenerForSingleValueEvent(new 
          ValueEventListener() {
                           @Override
                           public void onDataChange(@NonNull DataSnapshot 
                        dataSnapshot) {

                                            if (dataSnapshot.exists()) {

                                              for (DataSnapshot 
                dataSnapshot1:dataSnapshot.getChildren()) {

                                          name = 
                  dataSnapshot.child("Name").getValue().toString();

                                         holder.uName.setText(name);

           //                                   }





                 }



                           @Override
                           public void onCancelled(@NonNull DatabaseError 
               databaseError) {

                           }
                       });


                    }

                }
                @Override
                public void onCancelled(@NonNull DatabaseError 
               databaseError) {  
                }
            });



        }




        @NonNull
        @Override
        public ShowStudentByCourse.UserViewHolder 
           onCreateViewHolder(@NonNull ViewGroup parent, int i) {


            View view = 
     LayoutInflater.from(parent.getContext()).inflate(R.layout.add_student, 
       parent, false);
            return new ShowStudentByCourse.UserViewHolder(view);
          }

           };

        recyclerView.setAdapter(adapter);
        adapter.startListening();
         }




    public static class UserViewHolder extends RecyclerView.ViewHolder  {
    public CircleImageView profileImg;
    ListView listView;
    TextView uName,uStatus,Uid,uNameValue,uSubjectValue;
    View  mView;

    public UserViewHolder(View itemView) {
        super(itemView);
        mView = itemView;


        uName = mView.findViewById(R.id.category_name);
        Uid = mView.findViewById(R.id.id_Course);
         uStatus = mView.findViewById(R.id.course_code);
          uNameValue = mView.findViewById(R.id.course_code_text);
         uSubjectValue = mView.findViewById(R.id.course_name);
             }



        }

0 个答案:

没有答案