I'm trying to add a nice feature to my very first and elementary app that connects to Firestore database, gets all the names of the documents in the collection there and stores them in an ArrayList of strings. As shown below, the database is quite simple and contains one collection named pedestals
which includes only 3 documents. Each document has 4 fields (jack, jack_laser, name and sum):
Firestore snapshot of my database
By using pedestals.whereEqualTo("name", true).get() I guess a query for all the names is initialized but I'm not sure how to extract the names out of it. I watched many youtube videos and tried the google code-lab section for firestore, but each of them deals with querying for documents and storing their data - While here I don't want to get all the documents, only the values of their name fields.
Here is the relevant method code:
private void myFirstQuery2() {
// Get a reference to the pedestal collection
final CollectionReference pedestals = mFirestore.collection("pedestals");
// get all the names of the pedestals in firestore
pedestals.whereEqualTo("name", true).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
for (QueryDocumentSnapshot document : task.getResult()) {
// names.add(document.getString("name"));
names.add(document.getData().toString());
}
String str = Arrays.toString(names.toArray());
Log.e(TAG, "god job: " +str);
Toast.makeText(FileLoading.this, "success!", Toast.LENGTH_LONG).show();
}//end of onSuccess
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(FileLoading.this, "error!", Toast.LENGTH_LONG).show();
Log.e(TAG, "could not get the query");
}
});
};
As you may see, I tried to implement a for loop that stores each documentsnapshot (even though I'm looking for fields and not documentsm. Is it the problem?) in an array.
In the logcat terminal I get:
08-24 17:41:45.786 10738-10738/benda.leveling_v2 E/MyActivity: god job: []
which makes me understand that the arraylist is empty.
What am I doing wrong here?
whole
documents you meant the whole "staircase" of info. - Gaurav Mall