英文:
How can I add colection to Firestore Database as in image?
问题
I want to add colection to Firestore database like this image:
I want to add a symbol in ArrayList every time the function is called.
fun addsymbol() {
val arrayList = arrayListOf<String>()
arrayList.add(Coin.Coin_Info?.symbol!!)
val postHashMap = hashMapOf<String, Any>()
postHashMap.put("symbols", arrayList)
Firestore.collection("Users").document("User1").set(postHashMap, SetOptions.merge())
.addOnCompleteListener { task ->
if (task.isSuccessful) {
System.out.println("Succes ")
}
}.addOnFailureListener {
System.println(it.message.toString())
}
}
Function working but it's not adding symbols to ArrayList, it's updating the ArrayList. How can I solve it?
英文:
I want to add colection to Firestore database like this image:
I want to add a symbol in Arraylist every time the function is called.
fun addsymbol(){
val arrayList= arrayListOf<String>()
arrayList.add(Coin.Coin_Info?.symbol!!)
val postHashMap = hashMapOf<String, Any>()
postHashMap.put("symbols",arrayList)
Firestore.collection("Users").document("User1").set(postHashMap, SetOptions.merge())
.addOnCompleteListener { task->
if(task.isSuccessful){
System.out.println("Succes ")
}
}.addOnFailureListener {
System.out.println( it.message.toString())
}
}
Function working but It's not adding symbols to arraylist , It's updating arraylist . How can I solve it?
答案1
得分: 1
不需要读取文档中的数组以添加新符号。您可以简单地将FieldValue#arrayUnion()作为参数传递给更新方法,如下所示:
Firestore.collection("Users").document("User1").update("symbols", FieldValue.arrayUnion(Coin.Coin_Info?.symbol!!))
我还建议附加一个完整的监听器,以查看是否有什么问题。
英文:
There is no need to read the array in the document in order to add a new symbol. You can simply pass FieldValue#arrayUnion() as an argument to the update method like this:
Firestore.collection("Users").document("User1").update("symbols", FieldValue.arrayUnion(Coin.Coin_Info?.symbol!!))
I also recommend attaching a complete listener, to see if something goes wrong.
答案2
得分: 0
问题在于每次调用addsymbol
函数时,都会创建一个新的arrayList
实例,然后向其中添加一个符号。然而,您没有检索来自Firestore数据库的现有符号数组,并将新符号附加到其中。要解决这个问题,您需要检索现有的符号数组,将新符号附加到其中,然后使用新数组更新Firestore数据库。
请尝试以下代码:
fun addsymbol(){
Firestore.collection("User").document("Userid").get()
.addOnSuccessListener { documentSnapshot ->
if (documentSnapshot.exists()) {
val symbols = documentSnapshot.get("symbols") as ArrayList<String>
symbols.add(Coin.Coin_Info?.symbol!!)
val postHashMap = hashMapOf<String, Any>()
postHashMap.put("symbols", symbols)
Firestore.collection("User").document("Userid").set(postHashMap, SetOptions.merge())
.addOnCompleteListener { task ->
if (task.isSuccessful) {
System.out.println("Success")
}
}.addOnFailureListener {
System.out.println(it.message.toString())
}
} else {
System.out.println("Document does not exist")
}
}
.addOnFailureListener {
System.out.println(it.message.toString())
}
}
英文:
The issue is that every time you call the addsymbol
function, you are creating a new instance of the arrayList
and then adding a symbol to it. However, you are not retrieving the existing array of symbols from the Firestore database and appending the new symbol to it. To fix this, you need to retrieve the existing array of symbols, append the new symbol to it, and then update the Firestore database with the new array.
try the following:
fun addsymbol(){
Firestore.collection("User").document("Userid").get()
.addOnSuccessListener { documentSnapshot ->
if (documentSnapshot.exists()) {
val symbols = documentSnapshot.get("symbols") as ArrayList<String>
symbols.add(Coin.Coin_Info?.symbol!!)
val postHashMap = hashMapOf<String, Any>()
postHashMap.put("symbols", symbols)
Firestore.collection("User").document("Userid").set(postHashMap, SetOptions.merge())
.addOnCompleteListener { task ->
if (task.isSuccessful) {
System.out.println("Success")
}
}.addOnFailureListener {
System.out.println(it.message.toString())
}
} else {
System.out.println("Document does not exist")
}
}
.addOnFailureListener {
System.out.println(it.message.toString())
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论