嗨,我正在尝试从回收视图中删除 A 行,一旦将其从回收视图中删除,它也应该从 firebase 实时数据库中删除。我可以从回收视图中删除它,但是当我回到我的活动时,它会重新填充所有行,因为我不确定如何从 Firebase 中删除它。任何人都可以帮忙吗?
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();
}
};
}