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?