get / fetch data from boolean array - Firestore Android - 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.

Related

The best approach to store list in Firebase Firestore

I want to keep a list with the ID of the users who liked the specific object. To achieve that I created an array where I'm trying to keep that list.
I also want to display how many users like that object.
In mapper:
likes = getLikedListSize(it["userLikedOfferList"].toString())
then
private fun getLikedListSize(userList: String): String {
return userList.length.toString()
}
The problem is that function returns random numbers. For example in the array are two items, function return "8" etc.
What is a better approach to store list and get the size of it in Firestore?
When you are using the following method call:
getLikedListSize(it["userLikedOfferList"].toString())
It means that you are trying to pass to the getLikedListSize() method, the String representation of the array object. This representation is nothing else but the address of the object in the memory. This address it's a String that consists of 8 characters. That's the reason why, when you call .length you return the length of that String and not the actual length of the array. To solve this, simply pass the array, without converting it to a String:
getLikedListSize(it["userLikedOfferList"])
And change the method like this:
private fun getLikedListSize(userList: Array<String>): Int {
return userList.length
}
Now, when calling this method, you'll always get the number of elements that exist in the userLikedOfferList array.

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!

JSON array order

I am using PHP as a middleman to access a MySql database and it returns the result of the query as a json string using json_encode, then display it within the TableLayout of the app, this is why order is important so I can line up the data and the headers.
After some research I found out that json does not enforce order so any time I call new JSONArray(result) it scrambles the json returned by PHP. Is there any way to preserve the order of the returned string? Or maybe I'm using the incorrect data structure on either end.
Relevant PHP result:
[{"FIELD1":"vsa","FIELD2":"dfs","FIELD3":"dsfa","FIELD4":"adsf","FIELD5":"23","ZIPCODE":"asdf","USERNAME":"asd","PASSWORD":"as","DATE1":"dsfa"}]
Relevant Android Result After JSONArray(result):
[{"ZIPCODE":"asdf","DATE1":"dsfa","FIELD3":"dsfa","FIELD2":"dfs","FIELD5":"23","FIELD4":"adsf","USERNAME":"asd","FIELD1":"vsa","PASSWORD":"as"}]
I believe the reordering inside a JSON object is due to the fact that JSON objects are key/value pairs (not an indexed array), which by default are unordered. However, the JSON array is an ordered sequence of values (JSON objects).
Don't rely on order!
Source: http://www.ietf.org/rfc/rfc4627.txt
I've never seen new JSONArray(String) change the order of anything, and I've used it a lot. However, what you have seems to be an array of length 1. Using myJsonArray.getJsonObject(0).getString("ZIPCODE") should still return the correct data, and as long as you query in the correct order (FIELD1, FIELD2, FIELD3, etc), you should be fine.

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