I've been spending so much hours searching on how to get an array inside. I want to access each the element of an array and store it in a string like for example, String array =??? (I want to get the value).
To further understand my question. Let's say I have an array that contains all id and I want to check if there's an id existing and if so I want to make each element to be stored in a string (each element must be on different string) so I can use it later in my other activities.
Is there a way like using for loop?
String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
DocumentReference docRef = db.collection("Users").document(userId);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
String plan_id = document.get("plan_id").toString();
Log.d(TAG, "***Found field: " + plan_id);
} else {
Log.d(TAG, "***No such document");
}
} else {
Log.d(TAG, "***Get failed with ", task.getException());
}
}
});
I get the following output
***Found field: [3bTzKOmIS1zEHprJa1u0, 6nDgyEimKOnoPbZRsbmQ]
If you have a list type field in a document, it will be converted to a List type object in Java when you fetch it. What you're doing now is converting that List to a String, which is not very helpful. Just cast it to a List and iterate it like any other List:
List<Object> plan_id = (List<Object>) document.get("plan_id");
for (Object o : plan_id) {
Log.d(TAG, "List element " + o.toString());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.