0

I have an android project where I retrieve data from firebase. everything is working fine but when I delete object in firebase console, it does not reflect back in the app.

Here is the link of the image: Click here

So, suppose I delete david hafiz child node in firebase, it does not delete in the app. I have tried a lot but can't find the correct way. I am new to android programming and I hope somebody can help me. Thank You.

Update

 mDatabase = FirebaseDatabase.getInstance();
        mReference = mDatabase.getReference().child("Students").child("Marks");

        mReference.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                Log.d("value", "" + dataSnapshot);
                    fetchData(dataSnapshot);


            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                    fetchData(dataSnapshot);
            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {
            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
        return view;
     }


private void fetchData(DataSnapshot dataSnapshot) {
            StudentData value = dataSnapshot.getValue(StudentData.class);
            Log.v("StudentData Fragment", "" + dataSnapshot.getValue());

            // Get an iterator.
            Iterator<StudentData> ite = mMarksList.iterator();

            while (ite.hasNext()) {
                StudentData iteValue = ite.next();
                if (iteValue.equals(value))
                    ite.remove();
            }
            mMarksList.add(value);
            Collections.sort(mMarksList, new MarksComparator());

            String title = mReference.getKey();

            // specify an adapter
            mAdapter = new MyAdapter(getContext(), mMarksList, title);
            mAdapter.notifyDataSetChanged();
            mRecyclerView.setAdapter(mAdapter);
    }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Ron Daulagupu
  • 423
  • 6
  • 18
  • Please share the [minimum code that is needed to reproduce the problem](http://stackoverflow.com/help/mcve). With that it's much more likely that someone is able to help. – Frank van Puffelen Oct 15 '17 at 17:52
  • Ok, I will paste the code where I am adding the object in arraylist. thanks for suggestion. – Ron Daulagupu Oct 15 '17 at 17:57
  • Firebase-ui is a view binding library for Firebase officially support by the Firebase people them self the FirebaseRecyclerAdapter they have made solve this problem in a manner you wouldn't even have to consider it https://github.com/firebase/FirebaseUI-Android is great for any starter project – cutiko Oct 17 '17 at 19:02

2 Answers2

1

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

 ArrayList<String> mKeys = new ArrayList<String>();  


  @Override
  public void onChildAdded(DataSnapshot dataSnapshot, String s) {
       String key = dataSnapshot.getKey();
       mKeys.add(key);
       adapter.notifyDataSetChanged();

       }

  @Override
  public void onChildRemoved(DataSnapshot dataSnapshot) {
       String key = dataSnapshot.getKey();
       int index = mKeys.indexOf(key);
       mMarksList.remove(/*index*/); // or data

       adapter.notifyDataSetChanged();
       }

You might be interested how to update too.

  • I didn't get your answer properly. How can I get the getKey() value when the object is not present in the firebase console. Somehow even after deleting in firebase console, it still remains in the arraylist. – Ron Daulagupu Oct 15 '17 at 17:55
1

There are four relevant methods in ``:

  • onChildAdded
  • onChildRemoved
  • onChildChanged
  • onChildMoved

You only implemented onChildAdded, which is called in two cases:

  1. When you first attach the listener, onChildAdded is called for each existing child.
  2. When a child is later added, onChildAdded is called for that child.

When you delete a node from the Firebase console, the onChildRemoved method is called. But since you left that method empty, your app doesn't do anything when you remove data from the console.

To make your app behave correctly, you'll need to implement onChildRemoved. Typically this involves finding the UI element matching the snapshot and removing it.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807