2

Here is my code. I use this array list in my adapater for showing in listview

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

right here

        return arrayList;
    }

Before the return, my arraylist is empty. can you guys help me?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Silvia
  • 117
  • 1
  • 6

1 Answers1

3

I'm not sure how to resolve your empty list but you can populate your adapter after you add the item in the list, instead of returning your list, like:

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();
            }
        }
    });
}
Bianca Balan
  • 321
  • 5
  • 14