Data is getting added into list instead of getting updated in addChildEventListener's onChildChanged()

后端 未结 1 526
感动是毒
感动是毒 2020-12-12 06:49

I have some data in firebase which gets changed on some specific actions and I want that change to be shown in the app.

I have a RecyclerView in which a

相关标签:
1条回答
  • 2020-12-12 07:24

    You can store keys in order to update it e.g:

     ArrayList<String> mKeys = new ArrayList<String>();  
    
    databaseReference.child("uListings").child(AccessToken.getCurrentAccessToken().getUserId()).addChildEventListener(new ChildEventListener() {
                        @Override
                        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                            // recyclerview gets populated here
    
                            String key = dataSnapshot.getKey();
                            mKeys.add(key);
                            adapter.notifyDataSetChanged();
    
                        }
    
                        @Override
                        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                            if (dataSnapshot.getValue() != null) {
                                 .......
                                 //now we get the index to update specific value
                                  String key = dataSnapshot.getKey();
                                  int index = mKeys.indexOf(key);
                                  list.set(index, pHandler);
    
                                  adapter.notifyDataSetChanged();
                                  ....
                        }
    
    0 讨论(0)
提交回复
热议问题