'Remove a Row from recycle view and Firebase

Hi I am trying to remove A row from a recycle view and once it is removed from the recycle view it should also be removed from the firebase realtime database. I am able to remove it from the recycle view but when I go back into my activity it repopulates with all the rows as I am unsure on how to remove it from Firebase. Can anyone help?

public class FindAllContacts extends AppCompatActivity {
    RecyclerView recyclerView;
    RecyclerView.Adapter adapter;
    ArrayList<Contacts> contacts = new ArrayList<>();
    FirebaseAuth mAuth = FirebaseAuth.getInstance();
    FirebaseUser firebaseUser = mAuth.getCurrentUser();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_all);
        getSupportActionBar().setTitle("View All Contacts");
        getAllContacts();
    }

    public void getAllContatcs() {
        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Contacts");
        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for (DataSnapshot userSnapshot : snapshot.getChildren()) {
                    Contact contact = userSnapshot.getValue(Contact.class);
                    assert contact != null;
                    contatcs.add(contact);
                    recyclerView = findViewById(R.id.findAllRV);
                    recyclerView.setHasFixedSize(true);
                    recyclerView.setLayoutManager(new LinearLayoutManager(FindAll.this));
                    adapter = new MainAdapter(contacts);
                    new ItemTouchHelper(itemTouch).attachToRecyclerView(recyclerView);
                    recyclerView.setAdapter(adapter);
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Toast.makeText(FindAll.this, "Error Occurred: " + error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    ItemTouchHelper.SimpleCallback itemTouch = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT | ItemTouchHelper.LEFT) {
        @Override
        public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
            int position = viewHolder.getAdapterPosition();
            contatcs.remove(viewHolder.getAdapterPosition());
            adapter.notifyDataSetChanged();

        }
    };
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source