Displaying array in a firestore document onto a recycler view - android

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()

Related

How to map Firebase array to Kotlin list?

I want to map array from Firebase as a list of string in Kotlin
I have been searching for answer in may threads but they doesn't answer my question
Now I fetch whole collection and I get DocumentSnapshot. When I do
list = it["myArray"].toString()
I get string:
list = [text1, text2, text3]
How Can I convert it to list?
You can use split() like,
val newList = list.split(',')
It will split the strings from every occurrence of ',' in it, and will return a list containing all the spliced strings.

How to append an array into an array field in firestore?

Question
I have a collection named Users with a field named friendEmails which is an array that contains Strings.
I have a document with friendEmails = {joe#gmail.com, dan#gmail.com}, and I want to append newEmails = {mat#gmail.com, sharon#gmail.com} to it.
Problem
The only options I know for this are:
Reading the friendEmails array first, then adding the union of it and newEmails to the document.
Iterating over the elements of newEmails (let's and each iteration doing:
myCurrentDocumentReference.update(FieldValue.arrayUnion, singleStringElement);
(Since FieldValue.arrayUnion only lets me pass comma-separated elements, and all I have is an array of elements).
These two options aren't good since the first requires an unnecessary read operation (unnecessary since FireStore seems to "know" how to append items to arrays), and the second requires many write operations.
The solution I'm looking for
I'd expect Firestore to give me the option to append an entire array, and not just single elements.
Something like this:
ArrayList<String> newEmails = Arrays.asList("mat#gmail.com", "sharon#gmail.com");
void appendNewArray() {
FirebaseFirestore firestore = FirebaseFirestore.getInstance();
firestore.collection("Users").document("userID").update("friendEmails", FieldValue.arrayUnion(newEmails));
}
Am I missing something? Wouldn't this be a sensible operation to expect?
How else could I go about performing this action, without all the unnecessary read/write operations?
Thanks!
You can add multiple items to an array field like this using FieldValue.arrayUnion() as the value of a field update:
docRef.update("friendEmails", FieldValue.arrayUnion(email1, email2));
If you need to convert an ArrayList to an array for use with varargs arguments:
docRef.update("friendEmails", FieldValue.arrayUnion(
newEmails.toArray(new String[newEmails.size()])
));

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

How to retrieve last element from firestore Array in android?

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);

android - Grabbing collectionGroups in a query returning empty

I'm working on using a collectionGroup query and trying to pass it to a Recyclerview but it seems to always be coming up empty.
Firstly, my understanding is that collectionGroups used not to be so great but now I hear it's fixed and it allows a query to grab all documents under a collectionGroup name.
The query I show below doesn't grab anything and I can't figure out why. From the explanation in the documentation, it should be able to grab all bookPendingRequests items across all books(the parent Items) that have an owner of whoever is logged in.
String mAuthUser = FirebaseAuth.getInstance().getCurrentUser().getDisplayName();
the line above allows me to grab the logged in username
Query query = firestoreDB.collectionGroup("bookPendingRequests").whereEqualTo("mOwner", mAuthUser)
This line above is what i imagine should work.
code
FirestoreRecyclerOptions<BookRequestModel> options = new FirestoreRecyclerOptions.Builder<BookRequestModel>()
.setQuery(query, BookRequestModel.class)
.build();
mBookRequestsAdapter = new BookRequestsAdapter(options);
RecyclerView bookRequestListRecyclerView = findViewById(R.id.bookRequestListRecyclerViewId);
bookRequestListRecyclerView.setHasFixedSize(true);
bookRequestListRecyclerView.setLayoutManager(new LinearLayoutManager(this));
bookRequestListRecyclerView.setAdapter(mBookRequestsAdapter);
here's an image of what it looks link in firebase:
here's an image of my index for the collectionGroup
Right now I'm just trying to get the query to actually grab the "bookPendingRequests".
Any help or guidance is appreciated.

Categories

Resources