I have a schema like below in firestore:
I have document snapshot listener written to fetch the whole array via:
// inside snapshot listener
List<String> order_data= (List<String>) documentSnapshot.get("done"); // this returning whole array!
But i want only last element from the done array. Any help people?
If you want any data in a document, you have to read the entire document. There is no avoiding that.
If you already have the contents of a list field in a List object, then you can get the list item in that list using:
String last = order_data.get(order_data.size() - 1);
Related
Firestore Dadabase
I am having some trouble displaying an array of dates from a document onto a recyclerview. I am stuck on the query part. On previous work, I've done something like this
var absentQuery = db.collection("Users").whereArrayContains("absentDate", "1-1-2021")
In that code I pasted it would query all the documents in the collection Users and look for an array field. But this time, I only want the array from a specific document. How would I display the array absentDates in document j9gaXg4ywQYxYigirx09DUWuGP82 using a recyclerview?
UPDATE:
So I am still stuck on this problem. So I know the FirestoreRecyclerAdapter requires an FirestoreRecyclerOptions as an argument. So I would use FirestoreRecyclerOptions.Builder create the FirestoreRecyclerOptions. It seems like I would only be able to create one model class for every DocumentSnapshot. So the problem is the DocumentSnapshot has the array with the dates and each date would be a model. This is currently what I have for the FirestoreRecyclerOptions.Builder
FirestoreRecyclerOptions.Builder<AbsentDateData>()
.setQuery(query, SnapshotParser<AbsentDateData> {
val dates = it.get("absentDates") as List<String>
AbsentDateData(dates) }).build()
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
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!
i try to add a Map item to a certain position of my Array. To do so i tried this code:
String userid =FirebaseAuth.getInstance().getCurrentUser().getUid();
DocumentReference washingtonRef = db.collection("Users").document(userid);
// Atomically add a new region to the "regions" array field.
washingtonRef.update("shoppingLists[0]", FieldValue.arrayUnion("greater_virginia"));
}
Here is the structure where i have to add a value:
At this point i have to add the value
You can't index into array fields like this with Firestore. You can add or remove elements of an list type field by value using arrayUnion and arrayRemove, but you can't specify where that item is in the array. If you need more control over the order of the elements in the array, you will have to read the document first, modify the array in memory, then update that field back into the document.
I have a small issue with ArrayList. I have to fetch the document from the server.
The document contains 7 fields of data. I have to show the document names in the list view.
For this I have added different fields data to the different ArrayList. So when I click on the document name, based on the position of the document, I fetched the all fields data from the different Arraylist based on the position.
But Have a small issue using by using the above procedure. Is there any procedure that is not depend on the position, what I want is irrespective of position if I click on the Document,based on the keyword document data to be extract.
Any Help Appreciated.
Thanks in Advance.
I got your point. If you try to manage different ArrayLists then it would be difficult to manage it. I mean if you delete item from particular position from particular ArrayList then you will have to delete items from same position from other ArrayList, if you forgot to do so then it will be unbalanced.
Solution:
Instead feasible solution is to create ArrayList<Object> or ArrayList<HashMap<String,String>>, so your every item is the type of particular object, and every object contains detail and everything of particular items.
For example: ArrayList<Documents>, here ArrayList will contains list of Documents objects, and every objects contains values of 7 fields.
Its simply easy to define Documents class with getter/setter attributes.