Unable to remove exact name from the recyclerview when child is removed in Firebase

87 Views Asked by At

I have a RecyclerView in an activity and when the user opens this activity from their devices, there names are added to the recyclerview.

Here's my code:

        mDatabase.child(rID).addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                    Map<String, String> map = (Map<String, String>) dataSnapshot.getValue();
                    pA = map.get("pName");
                    uA = map.get("pUrl");
                    String cLatS = map.get("cLat").trim();
                    currentLtAU = Double.parseDouble(cLatS);
                    String cLngS = map.get("cLng").trim();
                    currentLnAU = Double.parseDouble(cLngS);

                    layout.setVisibility(View.VISIBLE);
                    progressBar.setVisibility(View.INVISIBLE);

                    prepareData();
                } else {
                    Snackbar snackbar = Snackbar
                            .make(coordinatorLayout, "Some error occurred. Please retry!", Snackbar.LENGTH_SHORT);
                    snackbar.show();
                    onBackPressed();
                }
            }

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

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

                Log.d("childRemoved", dataSnapshot.getKey());

                aModelClass = new AModelClass(pA, uA, String.valueOf(currentLtAU), String.valueOf(currentLnAU), null, s, v, availableFrom, rID, userID2, noa);
                aFastItemAdapter.remove(aFastItemAdapter.getAdapterItemCount() - 1);
                aList.setAdapter(aFastItemAdapter);
                aList.smoothScrollToPosition(0);

            }

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

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

Here's prepareData(); method :-

public void prepareData() {
        aModelClass = new AModelClass(pA, uA, String.valueOf(currentLtAU), String.valueOf(currentLnAU), null, s, v, availableFrom, rID, userID2, noa);
        aFastItemAdapter.add(aModelClass);
        aList.setAdapter(aFastItemAdapter);
        aList.smoothScrollToPosition(0);
}

The names are getting added but the problem is that they aren't getting removed exactly as removed in database. Like in database, "John" got removed but here in the list, the last item whatever it is gets removed.

How can I remove exactly the same name which was removed from the database from the list too?

1

There are 1 best solutions below

2
On

you have to refresh adapter after remove data from it.

aFastItemAdapter.remove(aFastItemAdapter.getAdapterItemCount() - 1);
notifyDataSetChanged();