Firebase recyclerview无法刷新

时间:2017-03-20 12:24:20

标签: android firebase firebase-realtime-database firebaseui

在尝试从Firebase数据库中读取数据后,我尝试根据新查询中的数据刷新RecyclerView。首次打开应用程序时,recyclelerview会根据初始查询填充项目,并且应该在之后立即刷新新查询。但是,在显示第一个查询的数据后,数据不会刷新.Am我做错了什么?请帮忙

users2 = (RecyclerView) findViewById(R.id.users_list);
    users2.setHasFixedSize(false);
    users2.setLayoutManager(new LinearLayoutManager(this));

    query = databaseReference;

     final FirebaseRecyclerAdapter<User, UserViewholder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<User, UserViewholder>(
            User.class, R.layout.users_row, UserViewholder.class, query) {
        @Override
        protected void populateViewHolder(UserViewholder viewHolder, User model, int position) {
            viewHolder.setUsername(model.getUsername());
            viewHolder.setLevels(model.getLevels());
            viewHolder.setDownloadUrl(getApplicationContext(), model.getDownloadUrl());
        }

    };

    users2.setAdapter(firebaseRecyclerAdapter);

    final String user_id = firebaseAuth.getCurrentUser().getUid();

    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot){

            halls2 = (String) dataSnapshot.child("Male").child(user_id).child("halls").getValue();
            halls1 = (String) dataSnapshot.child("Female").child(user_id).child("halls").getValue();



        }
        @Override
        public  void onCancelled(DatabaseError databaseError){

        }
    });

    databaseReference.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot){


            if(dataSnapshot.child("Male").hasChild(user_id)){

                 query = databaseReference.child("Female").orderByChild("halls").equalTo(halls2);

            }
            else{
                query = databaseReference.child("Male").orderByChild("halls").equalTo(halls1);

            }
            firebaseRecyclerAdapter.notifyDataSetChanged();


        }

        @Override
        public  void onCancelled(DatabaseError databaseError){

        }
    });

0 个答案:

没有答案