I want to store my database child in an array list of my model.
Model:
public class testInformation {
private String taskName;
private String answer;
public testInformation() {
}
public String getTaskName() {
return taskName;
}
public void setTaskName(String taskName) {
this.taskName = taskName;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
}
In the fragment's onCreate() method, I call a GetDataFromDatabase() method, which looks like this:
public void GetDataFromDatabase(){
mFirebaseDatabase= FirebaseDatabase.getInstance();
myref=mFirebaseDatabase.getReference().child("solutionKey");
myref.keepSynced(true);
myref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds:dataSnapshot.getChildren()){
testInformation testInformation=new testInformation();
testInformation.setTaskName(ds.getKey().toString());
testInformation.setAnswer(ds.getValue().toString());
results.add(testInformation);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
I have a public ArrayList<testInformation> variable called results, which is used to store the data from the database, but even after the GetDataFromDatabase() is called, the results list seems to be empty, but when i put a breakpoint at the OnDataChange() method, it seems to get filled up with the proper data.
My question is, how can I get the filled up results list from the OnDataChange() to work with it?
Edit: This list won't be modified or changed, so I only need to get it once, that's why I used the addListenerForSingleValueEvent() method.