On the online voting system that I am currently working on, I am preparing for the voting feature. I plan to remove an item from the RecyclerView that the user has already voted on but in a way that it does not remove it from the Firestore database itself. Is there a way to do that? If not, is there any workaround to make my concept work? Thank you in advance for the responses.
Asked
Active
Viewed 59 times
0
1 Answers
1
I plan to remove an item from the RecyclerView that the user has already voted on but in a way that it does not remove it from the database itself.
The simplest solution would be to add a field of type boolean with the default value of false, into each document. You can simply call it "deleted". When you want to perform a delete operation, don't remove the document from the database, rather change its value from false to true.
If you want to have a temporary removal, you might also consider moving a document from a location to another.
If not, is there any workaround to make my concept work?
You can also use Cloud Functions, to trigger a function that contains a particular logic, when a delete operation takes place.

Alex Mamo
- 130,605
- 17
- 163
- 193
-
Thank you for the wonderful response good sir! I will put these information you provided into good use :) – HeroreH29 Nov 10 '21 at 11:31
-
Good to hear that, HeroreH29. – Alex Mamo Nov 10 '21 at 11:39
-
1I will try everything first, and if it works, I will consider marking this answer as accepted :) – HeroreH29 Nov 10 '21 at 14:52
-
Sure. Give it a try and tell me if it works. – Alex Mamo Nov 10 '21 at 15:15
-
I got another problem. Since I created a field of type boolean in the DB, I queried it alongside documents that have registrationStatus = "Accepted" using .whereEqualTo After doing so, I can't see any documents filling up my RecyclerView but after removing the boolean from the query, thus reverting it, I can now see again the documents in my RecyclerView. Is there anything wrong in my coding when I query it beside registrationStatus = "Accepted"? – HeroreH29 Nov 10 '21 at 16:17
-
This is the code `Query query = notebookRef.whereEqualTo("registrationStatus", "Accepted").whereEqualTo("deleted", false).orderBy("candidateFullName", Query.Direction.ASCENDING);` – HeroreH29 Nov 10 '21 at 16:19
-
1You should create an [index](https://stackoverflow.com/questions/50305328/firestore-whereequalto-orderby-and-limit1-not-working). I have also answered a [question](https://stackoverflow.com/questions/69904475/error-is-coming-while-get-query-in-android-studio-firestore/69908942#69908942) on this topic earlier. Is it ok now? – Alex Mamo Nov 10 '21 at 18:20
-
1If you have some hard time implementing that, please post a new question, here on StackOverflow, using its own [MCVE](https://stackoverflow.com/help/mcve), so I and other Firebase developers can help you. – Alex Mamo Nov 10 '21 at 20:29
-
1I have created the index and now it works perfectly. Thank you good sir! – HeroreH29 Nov 11 '21 at 01:31