I want to update or create a field called "tags" inside a document, which should contain an array of custom objects called Tags ("tag_name", "tag_color"). To update or create such a field, I must pass an array of said object (not able to pass single objects, since it would be ineffective and expensive). How can I achieve this? I have tried adding an ArrayList
of Tag objects to a Hashmap:
HashMap<String, ArrayList<GenericTagModel>> myObject = new HashMap<>();
ArrayList<GenericTagModel> toArrayList = new ArrayList<GenericTagModel>(usedModels);
myObject.put("tags",toArrayList);
db.collection("users").document(user.getUid()).set(myObject, SetOptions.merge());
But all this does is add a field with nothing inside of it: "tags:[]"
What can I do?
You are getting tags:[]
because your toArrayList
empty, does not contain any GenericTagModel
objects. To solve this, please use the following lines of code:
HashMap<String, ArrayList<GenericTagModel>> myObject = new HashMap<>();
ArrayList<GenericTagModel> toArrayList = new ArrayList<GenericTagModel>(usedModels);
toArrayList.add(new GenericTagModel("redTag", "Red")); //Populate ArrayList
toArrayList.add(new GenericTagModel("blueTag", "Blue")); //Populate ArrayList
toArrayList.add(new GenericTagModel("greenTag", "Green")); //Populate ArrayList
myObject.put("tags",toArrayList);
db.collection("users").document(user.getUid()).set(myObject, SetOptions.merge());
The result will be an array that will contain theree GenericTagModel
objects.
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.