I have a an app with a functioning Next question button to proceed to the next question but I am having a problem when coding the "previous question" button.
I have coded it the same as the "next question" except that instead of incrementing (i++) I just decrement it (i--)
It does its job and it returns to the previous question...
But! when I press the previous button it does this.
example: I'm at question 4 and pressed previous, it goes to question 5 then if I click previous again it then goes back to question 4 and if I clicked it again then it will be a normal previous button. The same will also happen if I pressed previous then clicked next again but only in reverse.
NEXT QUESTION CODE
question = new Firebase("https://thesis-e4f56-default-rtdb.firebaseio.com/Set1/" + questionNumber + "/question");
question.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String question = dataSnapshot.getValue(String.class);
mQuestion.setText(question);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
questionum = new Firebase("https://thesis-e4f56-default-rtdb.firebaseio.com/Set1/" + questionNumber + "/num");
questionum.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String question = dataSnapshot.getValue(String.class);
nQuestion.setText(question);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
c1 = new Firebase("https://thesis-e4f56-default-rtdb.firebaseio.com/Set1/" + questionNumber + "/choice1");
c1.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String choice = dataSnapshot.getValue(String.class);
choice1.setText(choice);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
c2 = new Firebase("https://thesis-e4f56-default-rtdb.firebaseio.com/Set1/" + questionNumber + "/choice2");
c2.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String choice = dataSnapshot.getValue(String.class);
choice2.setText(choice);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
c4 = new Firebase("https://thesis-e4f56-default-rtdb.firebaseio.com/Set1/" + questionNumber + "/choice4");
c4.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String choice = dataSnapshot.getValue(String.class);
choice4.setText(choice);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
c5 = new Firebase("https://thesis-e4f56-default-rtdb.firebaseio.com/Set1/" + questionNumber + "/choice5");
c5.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String choice = dataSnapshot.getValue(String.class);
choice5.setText(choice);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
// answer = new Firebase( "https://thesis-e4f56-default-rtdb.firebaseio.com/"+questionNumber+"/answer");
// answer.addValueEventListener(new ValueEventListener() {
// @Override
// public void onDataChange(DataSnapshot dataSnapshot) {
// ans = dataSnapshot.getValue(String.class);
// }
// @Override
// public void onCancelled(FirebaseError firebaseError) {
// }
// });
if (questionNumber == 10) {
Intent intent = new Intent(depression_assessment.this, schoolwork_assessment.class);
startActivity(intent);
String Score = mScoreView.getText().toString();
// root.push().setValue(Score);
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Score");
ref.child(userId).child("result_general").setValue(Score);
}
// gameWon();
// storeScore();
questionNumber++;
if (questionNumber <=2) {
prev.setVisibility(View.VISIBLE);
}
if (questionNumber ==1) {
prev.setVisibility(View.INVISIBLE);
}
}
PREVIOUS QUESTION CODE
question = new Firebase("https://thesis-e4f56-default-rtdb.firebaseio.com/Set1/" + questionNumber + "/question");
question.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String question = dataSnapshot.getValue(String.class);
mQuestion.setText(question);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
questionum = new Firebase("https://thesis-e4f56-default-rtdb.firebaseio.com/Set1/" + questionNumber + "/num");
questionum.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String question = dataSnapshot.getValue(String.class);
nQuestion.setText(question);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
c1 = new Firebase("https://thesis-e4f56-default-rtdb.firebaseio.com/Set1/" + questionNumber + "/choice1");
c1.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String choice = dataSnapshot.getValue(String.class);
choice1.setText(choice);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
c2 = new Firebase("https://thesis-e4f56-default-rtdb.firebaseio.com/Set1/" + questionNumber + "/choice2");
c2.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String choice = dataSnapshot.getValue(String.class);
choice2.setText(choice);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
c4 = new Firebase("https://thesis-e4f56-default-rtdb.firebaseio.com/Set1/" + questionNumber + "/choice4");
c4.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String choice = dataSnapshot.getValue(String.class);
choice4.setText(choice);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
c5 = new Firebase("https://thesis-e4f56-default-rtdb.firebaseio.com/Set1/" + questionNumber + "/choice5");
c5.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String choice = dataSnapshot.getValue(String.class);
choice5.setText(choice);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
// gameWon();
// storeScore();
questionNumber--;
}
You increase the question number at the very last, that's the issue.
For example, let's say you loaded data of question 3, the index will now show question 4, your previous button will load question 4 THEN will decrement the index
Instead, the questionnumber must initiated at -1 (because 0 will be the first question index). Then the questionnumber++ must be the very first thing to do on code.
The same about questionnumber-- for the previous button.
This, the questionnumber will indicate the current question (you can do a +1 to show it to the user for a human readable), and it will not hold the next future question instead you have here.