TestActivity.java
public class TestActivity extends AppCompatActivity {
private DatabaseReference fireBaseRef = ConfigurationFirebase.getFirebaseDatabase();
private Button buttonTest;
private Question question;
List<Question> questionsList = new ArrayList();
private String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simulado);
generateQuestions();
buttonTest= findViewById(R.id.buttonTest);
buttonTest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(TestActivity.this, listaQuestion.get(0).getQuestion().toString(), Toast.LENGTH_LONG).show();
}
});
}
public List<Question> generateQuestions() {
DatabaseReference questionRef = fireBaseRef.child("questions");
questionRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
question = dataSnapshot.child("id").child("1").getValue(Question.class);
name = question.getQuestion().toString();
Question objectQuestion = new Question();
objectQuestion.setQuestion(name);
objectQuestion.setOptionA(question.getOptionA());
objectQuestion.setOptionB(question.getOptionB());
objectQuestion.setOptionC(question.getOptionC());
objectQuestion.setOptionD(question.getOptionD());
objectQuestion.setResult(question.getResult());
questionsList.add(objectQuestion);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
return questionsList;
}
}
I keep getting error java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
when i try to show a toast using the List, but with the variable name it shows. The class Question only have getters and setters