如何从onComplete返回String或Array? - Android - Firestore

时间:2017-12-28 22:00:07

标签: java android firebase google-cloud-firestore

我正在编写需要在 Firestore 数据库中获取数据的应用。我创建了一个显示结果的Recyclerview,但是当我填充它时,信息永远不会到来。我想是因为我在 onCompleteListener 中改变了 Cervezas 的值。

方法 cargarDatos()与方法 organizarDatos()的活动相同。

在我设置公共变量的方法之前:

public String Cervezas = "no information";

这是组织和填充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();

                }
            });


}

这是获取数据的代码:

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

一个问题是它没有给我任何错误,但是要么使用信息填充Recyclerview,因为 OnComplete 不会返回字符串。

我花了好几个小时看着这个,而且对于我所学到的,OnComplete只能返回void,但必须有一种方法来传递该变量!

我该怎么办?你呢!

0 个答案:

没有答案