从Firestore数组中删除元素 [英] Remove element from firestore array

查看:64
本文介绍了从Firestore数组中删除元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试从当前存储在Firestore中的数组中删除一个元素.

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

我已经检查了所有地方,甚至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());

我的Firebase结构-

My firebase structure -

推荐答案

如果要从数组中删除项目,则客户端应用程序需要传递该项目的整个对象值.如果仅传递索引或该项目中的嵌套值之一,则将无法正常工作.

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.

对于您的数据,这意味着您将需要使用该项目的确切内容(情感编号和文本字符串)构建一个HashMap,并将其传递给update().例如:

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.

这篇关于从Firestore数组中删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆