0

I have a firestore db and I am trying to get data from there.My purpose is getting data from there in my getDiaryInformation Class and put all datas to an Arraylist.After put all datas I want to send it to my Activity Class.But when I try the return my arraylist from my getDiaryInformation to my Activity class it returns empty even it has data inside of it.Simply I am adding my values to my Arraylist but can't send to my Activity class when I try to return it.What can I do for returning my values to my Activity class?

Here is my getDiaryInformation class.I put my title values to my arraylist.I am trying to send that arraylist to my activity class.

`

public class GetDiaryInformation {
    ArrayList<String> titleInformation=new ArrayList<>();
   

    public ArrayList<String> getTitle(){
        FirebaseFirestore db=FirebaseFirestore.getInstance();
        db.collection("diary").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if(task.isSuccessful()){
                    for (QueryDocumentSnapshot document:task.getResult()) {
                        titleInformation.add(document.get("title").toString());
                    }

                }
                else{
                    Log.d("EXCEPTION",task.getException().getMessage());

                }

            }

        });

        return titleInformation;
    }


}`

Here is I am trying to get values from getDiaryInformation class.After I get the values I want to print them but my arraylist returns empty.

`public class ReadDiary extends AppCompatActivity {
    GetDiaryInformation Data;
    ArrayList<String> titleInformation=new ArrayList<>();
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read_diary);
        getSupportActionBar().setTitle("Read Your Diaries");


        Data=new GetDiaryInformation();
        titleInformation=Data.getTitle();
        Log.d("NEPTUN", String.valueOf(titleInformation.size()));
        for (int i = 0; i < Data.titleInformation.size(); i++) {
            Log.d("MARS",titleInformation.get(i));
        }
        

    }





}`

I looked https://stackoverflow.com/questions/55319499/synchronized-java-oncomplete-method but couldn't understand what to do.Also tried to add interface like in that link but I am calling my method in my Activity class so it's another problem for me. What can I do to prevent that and get my all values to my Arraylist?

Ömer
  • 13
  • 5
  • There is no way you can return the `titleInformation` as a result o a method. Firebase API is asynchronous. So please check the duplicate to see how can you solve this using a callback. You might also be interested in reading this [resource](https://medium.com/firebase-tips-tricks/how-to-read-data-from-cloud-firestore-using-get-bf03b6ee4953). – Alex Mamo Dec 09 '22 at 08:23

1 Answers1

0

Try using @Autowire annotation for your dependency injection.

Instead Of:

GetDiaryInformation Data;

Try:

@Autowired
private GetDiaryInformation Data;

Or you could try:

private GetDiaryInformation getDiaryInformation;

    public ReadDiary (GetDiaryInformation getDiaryInformation) {
     this.getDiaryInformation = getDiaryInformation; 
}

https://www.baeldung.com/inversion-control-and-dependency-injection-in-spring

Dependency injection is a pattern we can use to implement IoC, where the control is inverted, setting an object's dependencies. Connecting objects with other objects, or “injecting” objects into other objects, is done by an assembler rather than by the objects themselves.

Gella
  • 1
  • 1
  • private GetDiaryInformation Data; is okey but still I can't get my variables.It still returns empty arraylist. – Ömer Dec 08 '22 at 16:02