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.
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()
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()])
));
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
I have one realm list and i want to sort the list alphabetically.
Collections.sort(contacts, new java.util.Comparator<Contacts>() {
#Override
public int compare(Contacts l1, Contacts l2) {
String s1 = l1.getName();
String s2 = l2.getName();
return s1.compareToIgnoreCase(s2);
}
});
But this logic not woking i am posting crash report below, please kindly go through it.
java.lang.UnsupportedOperationException: Replacing and element is not supported.
at io.realm.RealmResults$RealmResultsListIterator.set(RealmResults.java:826
at io.realm.RealmResults$RealmResultsListIterator.set(RealmResults.java:757)at java.util.Collections.sort(Collections.java:1909)
Please kindly go through my post and suggest me some solution.
Sorting in Realm is pretty straight forward.
Here is an example:
Let's assume you want to sort the contact list by name, you should sort it when your querying the results, you will get the results already sorted for you.
There are multiple ways to achieve this
Example:
1) Simple and Recommended way:
// for sorting ascending
RealmResults<Contacts> result = realm.where(Contacts.class).findAllSorted("name");
// sort in descending order
RealmResults<Contacts> result = realm.where(Contacts.class).findAllSorted("name", Sort.DESCENDING);
Updated since realm 5.0.0
1) Simple and Recommended way:
// for sorting ascending, Note that the Sort.ASCENDING value is the default if you don't specify the order
RealmResults<Contacts> result = realm.where(Contacts.class).sort("name").findAll();
// sort in descending order
RealmResults<Contacts> result = realm.where(Contacts.class).sort("name", Sort. DESCENDING).findAll();
2) for unsorted results
If you are getting unsorted results you can sort them anywhere this way.
RealmResults<Contacts> result = realm.where(Contacts.class).findAll();
result = result.sort("name"); // for sorting ascending
// and if you want to sort in descending order
result = result.sort("name", Sort.DESCENDING);
Have a look here you will find very detailed examples about Realm querying, sorting and other useful usage.
Technically you should use the following:
RealmResults<Contacts> result = realm.where(Contacts.class)
.findAllSorted("name", Sort.DESCENDING);
It is recommended over findAll().sort().
Use this to get the sorting in SORT_ORDER_ASCENDING or SORT_ORDER_DESCENDING
public void sort(java.lang.String[] fieldNames, boolean[] sortAscending)
Check the API reference Reference 1 or Reference 2
I want to group all contact in my app by using the first character of contact name. The result looks like deafault Contact Book in Android Phone.
I don't know the name of API which can solve my problem.
Could you give me the name of it?
Picture figures: image
You can use the an object of TreeSet class to sort strings as shown below:
TreeSet mySet = new TreeSet();
mySet.add("java");
mySet.add("C");
mySet.add("Pascal");
mySet.add("ruby");
Log.d("TAG",mySet);//output here will be C,java,Pascal,ruby,
//Now our task is to fetch strings from the sorted strings in `mySet` object
String[] names= mySet.toArray(new String[mySet.size()]);
//Now you will have sorted names in the names[] array
So, as commented if you are able to fetch string/names from your contacts, then you can use the above concept by creating an object of type TreeSet and adding all your contacts to this object and converting it to an array of strings as shown above.
I'm sure this one definitely helps.