【问题标题】:I want to load data from firestore cloud to listview in android studio using java我想使用java将数据从firestore云加载到android studio中的listview
【发布时间】:2021-05-29 13:56:50
【问题描述】:

这是我的代码 我在我的适配器中使用这个数组列表在列表视图中显示

 public ArrayList<String> loadFromFirebase() {
        ArrayList<String> arrayList = new ArrayList<>();
        db.collection("notite").whereEqualTo("User email", user.getEmail())
                .get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if(task.isSuccessful() && !task.getResult().isEmpty()) {
                    DocumentSnapshot documentSnapshot = task.getResult().getDocuments().get(0);
                    String documentId = documentSnapshot.getId();
                    db.collection("notite").document(documentId).get()
                            .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                                @Override
                                public void onSuccess(DocumentSnapshot documentSnapshot) {
                                    if(documentSnapshot.exists()) {
                                        String notita = documentSnapshot.getString("Detalii");
                                        arrayList.add(notita);
                                    } else {
                                        Toast.makeText(getContext(), "Notes does not exist",
                                                Toast.LENGTH_LONG).show();
                                    }
                                }
                            }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Toast.makeText(getContext(), "Failure retrieving",
                                    Toast.LENGTH_LONG).show();
                            Log.d("Retrieve", e.toString());
                        }
                    });
                } else {
                    Toast.makeText(getContext(), "Does not exist"
                            , Toast.LENGTH_LONG).show();
                }
            }
        });

就在这里

        return arrayList;
    }

在返回之前,我的arraylist 是空的。你们能帮帮我吗?

【问题讨论】:

  • Firebase API 是异步的。所以,这很可能是因为this
  • @AlexMamo 这解决了我的新问题。非常感谢!

标签: java android firebase google-cloud-firestore


【解决方案1】:

我不确定如何解决您的空列表,但您可以填充您的 在列表中添加项目后适配器,而不是返回您的 列表,例如:

public void loadFromFirebase() {
    ArrayList<String> arrayList = new ArrayList<>();
    db.collection("notite").whereEqualTo("User email", user.getEmail())
            .get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if(task.isSuccessful() && !task.getResult().isEmpty()) {
                DocumentSnapshot documentSnapshot = task.getResult().getDocuments().get(0);
                String documentId = documentSnapshot.getId();
                db.collection("notite").document(documentId).get()
                        .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                            @Override
                            public void onSuccess(DocumentSnapshot documentSnapshot) {
                                if(documentSnapshot.exists()) {
                                    String notita = documentSnapshot.getString("Detalii");
                                    arrayList.add(notita);
                                    arrayAdapter = new ArrayAdapter<>(getContext(),android.R.layout.simple_list_item_1,
                                            arrayList);
                                    listView.setAdapter(arrayAdapter);
                                } else {
                                    Toast.makeText(getContext(), "Notes does not exist",
                                            Toast.LENGTH_LONG).show();
                                }
                            }
                        }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(getContext(), "Failure retrieving",
                                Toast.LENGTH_LONG).show();
                        Log.d("Retrieve", e.toString());
                    }
                });

            } else {
                Toast.makeText(getContext(), "Does not exist"
                        , Toast.LENGTH_LONG).show();
            }
        }
    });
}

【讨论】:

    【解决方案2】:

    谢谢你,我!你应该得到一枚奖牌;)

    【讨论】:

    • 请在 cmets 部分提及 cmets。
    猜你喜欢
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    • 2020-08-15
    • 2015-12-01
    • 1970-01-01
    相关资源
    最近更新 更多