I'm having a problem creating an application that can interact with firebase and get information stored in the server. But I am facing some problem here at line 53. Please be informative as I don't know why this is happening. When I tried with offline direct "Questions", it worked completely fine. But when connecting the server, it fails every time.
//Line 53
questions = list.get(index);
This is what I did in the QuestionAnsActivity.java
...
import static com.company.appname.QuizSplash.list;
...
public class QuestionAnsActivity extends AppCompatActivity {
...
List<Questions> allQuestionsList;
Questions questions;
int index = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.question_ans_activity);
...
allQuestionsList = list;
Collections.shuffle(allQuestionsList);
// The below code is my line 53
questions = list.get(index);
}}
This is what I did in QuizSplash.java
public class QuizSplash extends AppCompatActivity {
public static ArrayList<Questions> list;
DatabaseReference databaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz_splash);
list = new ArrayList<>();
databaseReference = FirebaseDatabase.getInstance().getReference("Question");
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
Questions questions = dataSnapshot.getValue(Questions.class);
list.add(questions);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
Intent intent = new Intent(QuizSplash.this, QuestionAnsActivity.class);
startActivity(intent);
}
}
This is in Questions.java
public class Questions {
String question;
String option1;
String option2;
String option3;
String option4;
String answer;
//==================================================================================================
// Empty constructor
//==================================================================================================
public Questions() {
}
//==================================================================================================
// Constructor
//==================================================================================================
public Questions(String question, String option1, String option2,
String option3, String option4, String answer) {
this.question = question;
this.option1 = option1;
this.option2 = option2;
this.option3 = option3;
this.option4 = option4;
this.answer = answer;
}
//==================================================================================================
// Getter and setter methods start here
//==================================================================================================
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getOption1() {
return option1;
}
public void setOption1(String option1) {
this.option1 = option1;
}
public String getOption2() {
return option2;
}
public void setOption2(String option2) {
this.option2 = option2;
}
public String getOption3() {
return option3;
}
public void setOption3(String option3) {
this.option3 = option3;
}
public String getOption4() {
return option4;
}
public void setOption4(String option4) {
this.option4 = option4;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
}
CodePudding user response:
As I see, you have two classes. The QuizSplash class where you actually perform the database call to get the questions, and the QuestionAnsActivity class, where you assign the result that is coming from the database to an existing list called allQuestionsList
. But that's not how you should deal with the Realtime Database.
Don't forget that data is loaded from the Realtime Database asynchronous, meaning that any code that needs data from the database, needs to be inside the onDataChange()
method, or be called from there. So you cannot set the list
object public and static and simply use it in another activity. You need to wait for the data to complete loading. So you either use that list inside the callback, or you can use my answer from the following post:
Where I have explained how you can propagate the result using an interface. If you understand Kotlin, you might also be interested in reading the following article: