Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

java - why recyclerView not updated after pushing child to specific parent?

i tried to update recyclerView after adding child to specific parent. for some reason it doesn't updated and I can't figure out the reason. this is my database schema

database schema

here is two methods "retrieveChildrenFromDB" for retrieve specific parent's child's

 public void retrieveChildrenFromDB() {
    accountRef.orderByChild("id").equalTo(parentId).addListenerForSingleValueEvent(new 
 ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            //loop through accounts to find the parent with that id
            for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {

                //loop through parent children to add them to adapter ArrayList
                for (DataSnapshot userchildren: 
                 userSnapshot.child("children").getChildren()) {
                    Child child = userchildren.getValue(Child.class);

                    children.add(child);
                    childsAdapter.notifyDataSetChanged();
                    childsAdapter.notifyItemRangeChanged(0, children.size());
                    recyclerView.invalidate();
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            throw databaseError.toException();
        }
    });

    recyclerView.setAdapter(childsAdapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
}

and "addChildToParent" adding child to specific parent

    private void addChildToParent() {
    id=new Date().getTime()+"";
    accountRef.orderByChild("id").equalTo(parentId).addListenerForSingleValueEvent(new 
       ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
                if(CheckForfileds()) {

                    userSnapshot.getRef().child("children").push()
                            .setValue(new Child(parentId,id,childAge, "0",childName,
                                  "0","0", "0"));

                   

                    childsAdapter.notifyItemRangeChanged(0, children.size());
                    recyclerView.invalidate();
                    childsAdapter.notifyDataSetChanged();
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            throw databaseError.toException();
        }
    });
}
question from:https://stackoverflow.com/questions/65895319/why-recyclerview-not-updated-after-pushing-child-to-specific-parent

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I added this methods to my adapter class

   public void addItem(Child c){
    children.add(c);
    notifyDataSetChanged();
}

then I call it from addChildToParent() like this

         childsAdapter.addItem(newChild);
         childsAdapter.notifyDataSetChanged();

and it works fine!!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...