简体   繁体   中英

How to delete document from firestore in recycler view in android

How to delete a document from Firestore in recyclerView in android.

I have auto-generated documents id's I Want to delete that auto-generated id's data

I tried this but this is not working

private void removeData() {
     ProgressDialog dialog = new ProgressDialog(this);
     dialog.setCancelable(false);
     dialog.setMessage("Deleting this item...");
     dialog.show();

    CollectionReference colRef firestore.collection("topic");
    String id = colRef.document().getId();

    colRef.document(id).delete().addOnCompleteListener(task -> {
        if (task.isSuccessful()) {
            Toast.makeText(PracticeActivity.this, "Deleted Successfully", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(PracticeActivity.this, "Unable to delete!", Toast.LENGTH_SHORT).show();
            Log.d("Firebase", "onComplete: Error Unable to delete : " + task.getException());
        }
        dialog.dismiss();
    });

}

I got the success msg but when I see the database it is not deleted.

and If you know how to delete the document then you can post your answer according to you.

Edited!

在此处输入图像描述

How Can I also delete that Image that is stored in my Firebase Storage if I delete that document.

because if I am able to delete that document then what can I do with that Image so I want to delete that Image from my Firebase Storage database both how can I do that?

When you are using the following line of code:

String id = colRef.document().getId();

It means that you are generating a new document ID. When it comes to a delete operation you should specify the existing document ID inside a document() call:

colRef.document(id).delete().addOnCompleteListener(/* ... /*);

If you don't store that ID anywhere, then you should consider storing it as a property of the document.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM