I'm programming an app that needs to fetch data in the Firestore data base. I created a Recyclerview that shows the results, but when I populate it, the information never arrives. I think is because I change the value of Cervezas inside the onCompleteListener.
The method cargarDatos() is in the same activity that the method organizarDatos().
Before the methods I set the public variable:
public String Cervezas = "no information";
This is the code that organizes and populates the Recyclerviev:
private void cargarDatos() {
final String user = mAuth.getCurrentUser().getUid();
if(user.isEmpty()){
Intent start_intent = new Intent
(HistorialActivity.this,StartActivity.class);
start_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(start_intent);
finish();
}
mBaseDatos.collection("usuarios").document(user).collection("Historial")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
for (DocumentSnapshot doc:task.getResult()){
organizarDatos(doc,user);
Pedidos pedidos = new Pedidos(doc.getId(),
Cervezas,
doc.getString("Precio"));
Log.d(TAG, "2 " + Cervezas);
pedidosList.add(pedidos);
}
adaptador = new ListItemAdaptador
(HistorialActivity.this,pedidosList);
listItem.setAdapter(adaptador);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(HistorialActivity.this, ""+e.getMessage(),
Toast.LENGTH_SHORT).show();
}
});
}
And this is the code that fetches the data:
private String organizarDatos(DocumentSnapshot doc, String user) {
String idDoc = doc.getId();
DocumentReference refBarriles = mBaseDatos.collection("usuarios").document(user)
.collection("Historial").document(idDoc).collection("Barriles")
.document("barriles");
refBarriles.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document != null) {
Log.d(TAG, "DocumentSnapshot data: " + task.getResult().getData());
String barriles = "BARRILES" + "\n";
Cervezas = "";
String bock = String.valueOf(document.get("Bock"));
if (bock != null){
barriles += "Bock: " + bock + "\n";
}
String scotch = String.valueOf(document.get("Scotch Ale"));
if (bock != null){
barriles += "Sotch Ale: " + scotch + "\n";
}
if (barriles.length() > 8){
Cervezas += barriles;
}
Log.d(TAG, "1 " + Cervezas);
} else {
Log.d(TAG, "No existe ese documento");
}
} else {
Log.d(TAG, "get() fallo con la exepcion: ", task.getException());
}
}
});
return Cervezas;
}
The one problem is that it doesn't give me any errors, but either populates the Recyclerview with the information, because the OnComplete doesn't return the String.
I spent hours looking at this, and for what I've learned, the OnComplete only can return void, but there has to be a way to pass that variable!
What can I do? thak you!