Failing to Update Array in Firestore - android

I am trying to update Array in Firestore. From what I know you cant just append to the array in Firestore instead you need to replace the complete array with a new one.
Following is the code in Kotlin where I was trying to update the array:
holder.button.setOnClickListener {
pending.add(o[0])
Firebase.firestore.collection("profiles").document("YHMauLouORRtrYBV2h4dHJ5A0s72").update(
"Pending" , "$pending"
)
Here o[0] is the string I desire to Append to the already Existing Array named pending
Initialy in Firestore :-
Output I am getting :-
Output I desire:-
Basically While updating its converting the Array to a sting of list and pushing to Firestore. How can I get the Desired Output as shown in image ?
Thanks in Advance

That's not how to update an array in Firestore. To achieve that, you should use:
Firebase.firestore.collection("profiles")
.document("YHMauLouORRtrYBV2h4dHJ5A0s72")
.update("Pending", FieldValue.arrayUnion("Banana"))
When you're calling update without the second argument it means that you want to update a regular field.

Related

How to read array data from the firestore using kotlin?

I am new in android development and when I read array data from firestore using following code
val variable = arrayOf(document.get("restaurant"))
and then loop over the variable using code
varibale.forEach {
Log.d("someTag", ${it.toString()} + " is your data")
}
I get the result with square brackets at log as following
[somedata, somedata2] is your data
my problem is that forEach loop runs only once and I am not able to get the result (without square brackets) as following
somedata is your data
somedata2 is your data
I have 2 elements in my restaurant array in firestore
I will be very thankfull to any one who will help me.
You are actually wrapping an array/list into another array when using arrayOf, that's why you see those brackets. Instead, try casting your document.get("restaurant") and then looping directly through it.
arrayOf doesn't parse an array. It creates a new array using the elements you pass to it. That's not what you want. You should instead cast document.get("restaurant") to the type that you expect to get from Firestore.
If a field is an array of strings, then the SDK will give you a List<*>, and you will need to make sure each item in the list is a String, if that's what you stored in the array.
val variable = document.get("restaurant") as List<*>
// Iterate variable here, make sure to check or convert items to strings

Make Score History in Array Field Firestore

How to make history score in Array
I try u make score in array like this
this my firestore
And this is my code
String uid = auth.getCurrentUser().getUid();
muridref.document(uid).update("nilai", FieldValue.arrayUnion(skortampil));
When I get the same score the array field doesn't make new Array data,
without see data there or not in array
As mentioned in the documentation Update elements in an array about this behavior:
If your document contains an array field, you can use arrayUnion() and arrayRemove() to add and remove elements. arrayUnion() adds elements to an array but only elements not already present.
Considering that, it's working as expected, since it's not adding values that are equal. So, this means that you won't be able to add values that are equal using the method arrayUnion() directly.
This other question from the Community - accessible here - indicates that for you to achieve this goal, you will need to read all the values from the array in your client side, update your values in the array outside the database and then, writing/updating it back in the database.
Let me know if the information helped you!

ANDROID: How to iterate data in firebase when adding a new data?

I have a project that involves adding data to Firebase and every time I add a new data I want to see the result of iterating the child.
Like for example:
I have 10 data under wordbank and I want the new added data to be inserted at the end of the list. How do you solve this? Please help! Thank you very much!

get / fetch data from boolean array - Firestore Android

firestoredatabase
Hi! i want to get data from an array of boolean from my firestore data base, if i run this code:
Log.d("firestore", String.valueOf(document.getData()));
I get this result:
{nombre=AVL, boolArray=[true, false, false]}
document.get("boolArray") should return a List<Boolean> type object.
document.getData() will return a Map which in term contains two other maps. In the first case, the value is of type String while in the second case, the value is an Array. But if you are using document.get("boolArray"), as Doug said, even if we know that data is stored as an array, the data is returned as a List<Boolean>.
If you want to read more, you can also see my answer from this post.

how can I put the data retrieved in a string array

I am working on an app, which takes data from the user, stores the data in the database. The data is retrieved using cursor, and should be put in a string array. I am successful till the retrieving part, but how can I put the data in a string array? and is using string array the right approach here?, I mean data is being added continuously and the string array size is fixed. Not sure what to do here, please help.
Okay found a solution here http://www.anddev.org/working%5Fwith%5Fthe%5Fsqlite-database%5F-%5Fcursors-t319.html
, using ArrayList would help here.
#sam You got to use ArrayList<String> for ur requirements.u can add ur retrieved data into ArrayListby this method:
arrayList.add(<ur retrieved string>);

Categories

Resources