0

I am currently trying to remove an element from the array currently stored in firestore.

I have checked everywhere even the firestore documentation and what they suggest is not working. This is currently my code and so far it does nothing when trying to delete from listview.

         Map<String, Object> note = new HashMap<>();
         note.put("posts", FieldValue.delete());
         documentReference.update("posts", FieldValue.arrayRemove(listView.getItemAtPosition(position).toString()));
         usersEmotions.remove(listView.getItemAtPosition(position).toString());
         listView.setAdapter(listAdapter);
         listAdapter.remove(listView.getItemAtPosition(position).toString());

My firebase structure - enter image description here

1 Answer 1

0

If you want to remove an item from an array, your client app needs to pass the entire object value of the item. It won't work if you just pass an index or one of the nested values in that item.

For your data, that means you will need to build a HashMap with the exact contents of the item (both emotion number and text string), and pass that to update(). For example:

HashMap<String, Object> map = new HashMap<>();
map.put(emotion, ...);
map.put(text, "...");
documentReference.update(posts, FieldValue.arrayRemove(map));

If you don't know the entire item's contents, this won't work. Your only other options is to read the document, modify the list in memory, and write it modified list back to the document.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.