Hi everyone I need help.
I have this code
I have 50 question string and I want if already 10 question appears then the game finish. thank you for your help
private Question mQuestion = new Question();
private String mAnswer;
private int mScore = 0;
private int mQuestionLenght = 5 ;
Random r;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
r = new Random();
answer1 = (Button) findViewById(R.id.answer1);
answer2 = (Button) findViewById(R.id.answer2);
answer3 = (Button) findViewById(R.id.answer3);
answer4 = (Button) findViewById(R.id.answer4);
score = (TextView) findViewById(R.id.score);
question = (TextView) findViewById(R.id.question);
score.setText("Score: " + mScore );
updateQuestion(r.nextInt(mQuestionLenght));
answer4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(answer4.getText() == mAnswer){
mScore++;
score.setText("Score: " + mScore);
updateQuestion(r.nextInt(mQuestionLenght));
} else {
gameOver();
}
}
});
}
private void updateQuestion(int num){
question.setText(mQuestion.getQuestion(num));
answer1.setText(mQuestion.getChoice1(num));
answer2.setText(mQuestion.getChoice2(num));
answer3.setText(mQuestion.getChoice3(num));
answer4.setText(mQuestion.getChoice4(num));
mAnswer = mQuestion.getCorrectAnswer(num);
}
private void gameOver(){
}
i have 50 question i want if user already answer 10 question game stop and show score. in that code it cant stop if they wrong answer game can stop but if user always right game load all question
In your Acitivty, add a counter attribute
private int numberOfQuestionsAsked = 0;
After each question asked, add 1 to your counter
if(answer4.getText().equals(mAnswer)){ //note : use .equals() and not == !
mScore++;
numberOfQuestionsAsked++;
score.setText("Score: " + mScore);
updateQuestion(r.nextInt(mQuestionLenght));
}
After the user answered a question, check if the counterhas reached 10, if yes, go to gameOver
if(numberOfQuestionsAsked <= 10) {
gameOver();
}
In gameOver, reset the counter so the game can restart
numberOfQuestionsAsked = 0;
Your code should look like
private Question mQuestion = new Question();
private String mAnswer;
private int mScore = 0;
private int mQuestionLenght = 5 ;
private int numberOfQuestionsAsked = 0;
Random r;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
r = new Random();
answer1 = (Button) findViewById(R.id.answer1);
answer2 = (Button) findViewById(R.id.answer2);
answer3 = (Button) findViewById(R.id.answer3);
answer4 = (Button) findViewById(R.id.answer4);
score = (TextView) findViewById(R.id.score);
question = (TextView) findViewById(R.id.question);
score.setText("Score: " + mScore );
updateQuestion(r.nextInt(mQuestionLenght));
answer4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(answer4.getText().equals(mAnswer)){ //note : use .equals() and not == !
mScore++;
score.setText("Score: " + mScore);
updateQuestion(r.nextInt(mQuestionLenght));
numberOfQuestionsAsked++;
} else {
gameOver();
}
if(numberOfQuestionsAsked <= 10) {
gameOver();
}
}
});
}
private void updateQuestion(int num){
question.setText(mQuestion.getQuestion(num));
answer1.setText(mQuestion.getChoice1(num));
answer2.setText(mQuestion.getChoice2(num));
answer3.setText(mQuestion.getChoice3(num));
answer4.setText(mQuestion.getChoice4(num));
mAnswer = mQuestion.getCorrectAnswer(num);
}
private void gameOver(){
numberOfQuestionsAsked = 0;
}
Add a counter in your code like this :
Int counter = 0;
if(counter <= 10 ){
updateQuestion(r.nextInt(mQuestionLenght));
counter++;
} else {
gameOver();
}
Add this and check, hope it will work.
First of all, I would use:
View.OnClickListener listener = new View.onClickListener() {
#Override
public void onClick(View view) {
if(view instanceOf (TextView) && ((TextView)view).getText().toString().equals(mAnswer)){
mScore++;
score.setText("Score: " + mScore);
if(mScore >= 10) {
gameCompleted();//ToDo
} else {
updateQuestion(r.nextInt(mQuestionLenght));
}
} else {
gameOver();
}
}
};
Then, use this listener in every answer.
Futhermore, your random number may fail because it can be higher than 50 and can be a repeated answer and your text comparison is not recommended, you could use an object which assigns an id to the text.
Enjoy coding.
Related
I'm almost new to android and trying to write a calculator code.
It's almost done but I want to try some other options for that
when I repeat clicking on equal button,I want to repeat last calculation entered.for example 3+5=8 then if I click on exe button again it would be 13(3+5+5)
and so on(18,23,...)
Also I have problem with entering more than two numbers.for example when I enter (4+5+6)and then click equal the answer will appear 11 and just calculates last two numbers entered.I want it to show the result of first two numbers then get other numbers.example:for(2+3*4-5/)->the output will be equal to (5->20->15)
Here is my code;If any one can help,I will be grateful! :) thanks
package com.example.sony.calculator;
public class MainActivity extends AppCompatActivity {
Float firstNumber,secondNumber,result;
Float thirdNumber;
TextView display;
Button one,two,three,four,five,six,seven,eight,nine,zero,exe,clear,multiply,divide,sum,minus;
boolean isSum,isMinus,isMultiply,isDivide,isEqual;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = (TextView) findViewById(R.id.khali);
one = (Button) findViewById(R.id.adade1);
two = (Button) findViewById(R.id.adade2);
three = (Button) findViewById(R.id.adade3);
four = (Button) findViewById(R.id.adade4);
five = (Button) findViewById(R.id.adade5);
six = (Button) findViewById(R.id.adade6);
seven = (Button) findViewById(R.id.adade7);
eight = (Button) findViewById(R.id.adade8);
nine = (Button) findViewById(R.id.adade9);
zero = (Button) findViewById(R.id.adade0);
multiply = (Button) findViewById(R.id.zarb);
divide = (Button) findViewById(R.id.taghsim);
sum = (Button) findViewById(R.id.jam);
minus = (Button) findViewById(R.id.menha);
exe = (Button) findViewById(R.id.mosavi);
clear = (Button) findViewById(R.id.pak);
final Button[] operators = new Button[5];
operators[0] = multiply;
operators[1] = divide;
operators[2] = sum;
operators[3] = minus;
operators[4] = exe;
final Button[] numbers = new Button[10];
numbers[0] = zero;
numbers[1] = one;
numbers[2] = two;
numbers[3] = three;
numbers[4] = four;
numbers[5] = five;
numbers[6] = six;
numbers[7] = seven;
numbers[8] = eight;
numbers[9] = nine;
for (int a = 0; a < 10; a++) {
final int finalA = a;
numbers[a].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
display.setText(display.getText().toString() + String.valueOf(finalA));
}
});
}
clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
display.setText("");
}
});
sum.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (display.getText() == "") {
display.setText("");
return;
}
else{
firstNumber = parseFloat(display.getText().toString());
isSum=true;
display.setText("");
return;
}
}
});
minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (display.getText()==""){
display.setText("");
}else {
firstNumber = parseFloat(display.getText().toString());
isMinus = true;
display.setText("");
}
}
});
multiply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (display.getText()==""){
Toast Error1=Toast.makeText(MainActivity.this,"Error;Please enter right format",Toast.LENGTH_SHORT);
Error1.show();
}else {
firstNumber = parseFloat(display.getText().toString());
isMultiply = true;
display.setText("");
}
}
});
divide.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (display.getText() == "") {
Toast Error1 = Toast.makeText(MainActivity.this, "Error;Please enter right format", Toast.LENGTH_SHORT);
Error1.show();
} else {
firstNumber = parseFloat(display.getText().toString());
isDivide = true;
display.setText("");
}
}
});
exe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
isEqual=true;
if (display.getText() == "") {
display.setText("");
}
else {
secondNumber = parseFloat(display.getText().toString());
if (isSum == true) {
result = firstNumber + secondNumber ;
display.setText(String.valueOf(result));
isSum=false;
return;
}
if (isMinus == true) {
result = firstNumber - secondNumber;
display.setText(String.valueOf(result));
isMinus = false;
return;
}
if (isDivide == true) {
result = firstNumber / secondNumber;
display.setText(String.valueOf(result));
isDivide = false;
return;
}
if (isMultiply == true) {
result = firstNumber * secondNumber;
display.setText(String.valueOf(result));
isMultiply=false;
return;
}
}
}
});
}
}
I am building a little quiz app and let's say on Question 1, I select option B, then submit and the quiz gives me the next question. However for question 2 if I try to select B, the RadioButton quickly unchecks itself and it is completely uncheckable, until I select another radio button and then try B again. The pattern is, whatever option I selected in the previous question, is uncheckable in the next question unless I click on a different radiobutton and then try again. I'm attaching my code. Any help please?
public class MainActivity extends AppCompatActivity {
QuestionBank allQuestions = new QuestionBank();
String pickedAnswer = "", correctAnswer = "";
final int numberOfQuestions = allQuestions.list.size();
int questionNumber = 0;
boolean noSelection = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nextQuestion();
}
private void nextQuestion() {
if (questionNumber <= numberOfQuestions - 1) {
TextView questionLabel = (TextView) findViewById(R.id.question_text_view);
String fullQuestion = allQuestions.list.get(questionNumber).questionSet.get("question").toString();
fullQuestion += "\n\na) " + allQuestions.list.get(questionNumber).questionSet.get("a");
fullQuestion += "\nb) " + allQuestions.list.get(questionNumber).questionSet.get("b");
fullQuestion += "\nc) " + allQuestions.list.get(questionNumber).questionSet.get("c");
fullQuestion += "\nd) " + allQuestions.list.get(questionNumber).questionSet.get("d");
correctAnswer = allQuestions.list.get(questionNumber).questionSet.get("answer").toString();
questionLabel.setText(fullQuestion);
questionNumber++;
} else {
restart();
}
}
public void getSelectedAnswer() {
RadioButton radio_1 = (RadioButton) findViewById(R.id.option1_button);
RadioButton radio_2 = (RadioButton) findViewById(R.id.option2_button);
RadioButton radio_3 = (RadioButton) findViewById(R.id.option3_button);
RadioButton radio_4 = (RadioButton) findViewById(R.id.option4_button);
if (radio_1.isChecked()) {
pickedAnswer = "a";
radio_1.setChecked(false);
} else if (radio_2.isChecked()) {
pickedAnswer = "b";
radio_2.setChecked(false);
} else if (radio_3.isChecked()) {
pickedAnswer = "c";
radio_3.setChecked(false);
} else if (radio_4.isChecked()) {
pickedAnswer = "d";
radio_4.setChecked(false);
} else {
noSelection = true;
}
}
public void submitAnswer(View view) {
getSelectedAnswer();
if (noSelection) {
AlertDialog.Builder a_builder = new AlertDialog.Builder(this);
a_builder.setMessage("Please select an answer!");
a_builder.show();
noSelection = false;
} else {
checkAnswer();
nextQuestion();
}
}
public void checkAnswer() {
if (correctAnswer == pickedAnswer) {
AlertDialog.Builder a_builder = new AlertDialog.Builder(this);
a_builder.setMessage("Right Answer!");
a_builder.show();
} else {
AlertDialog.Builder a_builder = new AlertDialog.Builder(this);
a_builder.setMessage("Wrong Answer!");
a_builder.show();
}
pickedAnswer = "";
correctAnswer = "";
}
public void restart() {
questionNumber = 0;
//Collections.shuffle(allQuestions.list);
nextQuestion();
}
}
call setChecked(false) on all the buttons after submitting or before showing next question
So I'm new to java and I'm trying to make quiz app as exercise. I kinda made one but it doesn't work:
public class QuizActivity extends AppCompatActivity {
TextView QuestionText;
Button button1;
Button button2;
Button button3;
Button button4;
ArrayList<Question> listOfQuestions;
int currentQuestion = 0;
Context context = this;
int NumberOfQuestions;
GameCreator game;
String totalCorrect = "";
String totalWrong = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
QuestionText = (TextView) findViewById(R.id.textJautajums);
button1 = (Button) findViewById(R.id.buttonOpcija1);
button2 = (Button) findViewById(R.id.buttonOpcija2);
button3 = (Button) findViewById(R.id.buttonOpcija3);
button4 = (Button) findViewById(R.id.buttonOpcija4);
NumberOfQuestions = Integer.parseInt(context.getString(R.string.JautajumuSkaits).toString());
game = new GameCreator(NumberOfQuestions);
listOfQuestions = game.makeQuestions();
Resources r = getResources();
int px1 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 165, r.getDisplayMetrics());
button1.setWidth(px1);
button2.setWidth(px1);
button3.setWidth(px1);
button4.setWidth(px1);
Resources e = getResources();
int px2 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 125, r.getDisplayMetrics());
button1.setHeight(px2);
button2.setHeight(px2);
button3.setHeight(px2);
button4.setHeight(px2);
if (currentQuestion == 0){
setQuestion(listOfQuestions.get(0));
}
button1.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View V){
gajiens(button1.getText().toString(), listOfQuestions.get(currentQuestion));
currentQuestion++;
}
}
);
button2.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View V){
gajiens(button2.getText().toString(), listOfQuestions.get(currentQuestion));
currentQuestion++;
}
}
);
button3.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View V){
gajiens(button3.getText().toString(), listOfQuestions.get(currentQuestion));
currentQuestion++;
}
}
);
button4.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View V){
gajiens(button4.getText().toString(), listOfQuestions.get(currentQuestion));
currentQuestion++;
}
}
);
}
public void gajiens(String answer, Question thisQuestion){
if (currentQuestion < 14){
if (answer.equals(thisQuestion.getAnswer())){
totalCorrect += "Question: " + thisQuestion.getQuestion() + "\nYour Answer: " + answer + "\n";
} else {
totalWrong += "Question: " + thisQuestion.getQuestion()) + "\nYour Answer: " + answer + "\n";
}
currentQuestion++;
setQuestion(listOfQuestions.get(currentQuestion));
} else {
Intent intent = new Intent(this, EndActivity.class);
intent.putExtra("correct", totalCorrect);
intent.putExtra("wrong", totalWrong);
startActivity(intent);
}
}
public void setQuestion(Question kursh){
QuestionText.setText(kursh.getJautajums());
button1.setText(kursh.getOption1());
button2.setText(kursh.getOption2());
button3.setText(kursh.getOption3());
button4.setText(kursh.getOption4());
}
object Question is:
public Question(String Question, String Option1, String Option2, String Option3,String Option4, String correctAnswer){
Question = Question;
Option1 = Option1;
Option2 = Option2;
Option3 = Option3;
Option4 = Option4;
correctAnswer = correctAnswer;
}
Basically the problem is that App doesn't count the right answers. For some reason it most of the time uses the original text of TextView as ''correctAnswer''. Anyone has any idea what to do? I suspect since this isn't working properly this isn't particularly best approach so maybe someone can suggest a better one?
to compare the value of a TextView with a String you can do this as below
TextView tvAnswer = (TextView) findViewById(R.id.tvAnswer);
String correctAnswer = "Correct Answer";
if(correctAnswer.equals(tvAnswer.getText.toString()) )
{
//do something
}
else{
//do something
}
I got a problem with CountDownTimer's StartButton: the timer doesn't start after pressing the button. How do I fix that?
I want to start the timer by pressing button buttonCount.
Can someone help me please?
int clicks = 0;
TextView textCount;
ImageButton buttonCount;
int guessCount =0;
boolean started = false;
boolean timerProcessing = false;
private CountDownTimer count;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_newgame);
count = new CountDownTimer(15000, 1000) {
public void onTick(long millisUntilFinished) {
int seconds = (int) ((millisUntilFinished / 1000));
textic.setText("Time Left: " + millisUntilFinished / 1000);
}
public void onFinish() {
textic.setText("Time's Up!");
buttonCount.setEnabled(false);
if (clicks > oldscore)
getSharedPreferences("myPrefs", MODE_PRIVATE).edit().putInt("highscore", clicks).commit();
}
};
final int oldscore = getSharedPreferences("myPrefs", MODE_PRIVATE).getInt("highscore", 0);
final TextView textView = (TextView) findViewById(R.id.applesEaten);
buttonCount = (ImageButton) findViewById(R.id.button);
buttonCount.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
clicks++;
textView.setText("Clicks: " + clicks);
TextView textView = (TextView) findViewById(R.id.topScoreView);
textView.setText("Best: " + oldscore);
if(!started){
count.start();
started = true;
timerProcessing = true;
}
}
});
final TextView textic = (TextView) findViewById(R.id.textView2);
}
It seems to me this is what you really want to do:
private int clicks = 0;
private TextView textCount;
private ImageButton buttonCount;
private int guessCount = 0;
private CountDownTimer count; // RENAMED
private boolean started = false; // FALSE.
private boolean timerProcessing = false;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_newgame);
count = new CountDownTimer(15000, 1000) { // MOVED UP
public void onTick(long millisUntilFinished) {
int seconds = (int) ((millisUntilFinished / 1000));
textic.setText("Time Left: " + millisUntilFinished / 1000);
}
public void onFinish() {
textic.setText("Time's Up!");
buttonCount.setEnabled(false);
if (clicks > oldscore)
getSharedPreferences("myPrefs", MODE_PRIVATE).edit().putInt("highscore", clicks).commit();
}
};
final int oldscore = getSharedPreferences("myPrefs", MODE_PRIVATE).getInt("highscore", 0);
final TextView textView = (TextView) findViewById(R.id.applesEaten);
buttonCount = (ImageButton) findViewById(R.id.button);
buttonCount.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
clicks++;
textView.setText("Clicks: " + clicks);
TextView textView = (TextView) findViewById(R.id.topScoreView);
textView.setText("Best: " + oldscore);
if(!started){
count.start(); // START COUNTDOWN TIMER
started = true;
timerProcessing = true;
}
}
});
final TextView textic = (TextView) findViewById(R.id.textView2);
}
And if you really want to start another CountDownTimer than the one you create at the bottom (named count). Then you need to instantiate it and set its behaviour, just like you do for the other CountownTimer.
Also, all the variables you use need to be created before (textic, oldscore)
I have been working this quiz for a little while, however i am struggling to match the the question with the answer.
The following line and the others which are supposed to be display the answer actually display "[[ljava.lang.string;#40585b18" and slight variations.
quesAns4.setText("4) " + answers[3]) ;
i have tried changing the line now above to:
quesAns4.setText("4) " + answers[0][3]);
Obviously i want the answers to match the questions and the method above only displays 8 from array
{"3","5","8","9"}
So basically yeah for each change of question i want them to match. If the questions is "In seconds, how long does it take for a F1 car to stop when travelling at 300km/h?" the answers possible to be displayed should be 4,6,8,10 etc.
Any help/guidance would be appreciated thanks.
Full code below!
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MathsMultiplicationActivity extends Activity {
TextView quesnum;
TextView ques;
TextView anst;
TextView ans1;
TextView ans2;
TextView ans3;
TextView ans4;
ImageView cross;
ImageView tick;
Button nxt;
int qno = 1;
int right_answers = 0;
int wrong_answers = 0;
int rnd1;
int rnd2;
String [] questions = {"How much mph does the F-Duct add to the car?",
"What car part is considered the biggest performance variable?",
"What car part is designed to speed up air flow at the car rear?",
"In seconds, how long does it take for a F1 car to stop when travelling at 300km/h?",
"How many litres of air does an F1 car consume per second?",
"What car part can heavily influence oversteer and understeer?",
"A third of the cars downforce can come from what?",
"Around how much race fuel would be consumed per 100km?","The first high nose cone was introduced when?",
"An increase in what, has led to the length of exhaust pipes being shortened drastically?"};
String [] [] answers = {{"3","5","8","9"},
{"Tyres","Front Wing","F-Duct","Engine"},
{"Diffuser","Suspension","Tyres","Exhaust"},
{"4","6","8","10"},
{"650","10","75","450"},
{"Suspension","Tyres","Cockpit","Chassis"},
{"Rear Wing","Nose Cone","Chassis","Engine"},
{"75 Litres","100 Litres","50 Litres","25 Litres"},
{"1990","1989","1993","1992"},
{"Engine RPM","Nose Cone Lengths","Tyre Size","Number of Races"}};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.multiplechoice);
// Importing all assets like buttons, text fields
quesnum = (TextView) findViewById(R.id.questionNum);
ques = (TextView) findViewById(R.id.question);
anst = (TextView) findViewById(R.id.answertit);
ans1 = (TextView) findViewById(R.id.answer1);
ans2 = (TextView) findViewById(R.id.answer2);
ans3 = (TextView) findViewById(R.id.answer3);
ans4 = (TextView) findViewById(R.id.answer4);
nxt = (Button) findViewById(R.id.btnNext);
cross = (ImageView) findViewById(R.id.cross);
tick = (ImageView) findViewById(R.id.tick);
cross.setVisibility(View.GONE);
tick.setVisibility(View.GONE);
quesnum.setText("Question: " + qno + "/10");
final Button buttonAbout = (Button) findViewById(R.id.btnNext);
buttonAbout.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
next();
}
private void next() {
qno++;
change_question();
}
private void change_question() {
if(tick.getVisibility() == View.VISIBLE){
right_answers++;
}
if(cross.getVisibility() == View.VISIBLE){
wrong_answers++;
}
if(qno==questions.length){
}else{
cross.setVisibility(View.GONE);
tick.setVisibility(View.GONE);
rnd1 = (int)Math.ceil(Math.random()*3);
rnd2 = (int)Math.ceil(Math.random()*questions.length)-1;
ques.setText(questions[rnd2]);
if(questions[rnd2]=="x")
{
change_question();
}
}
questions[rnd2]="x";
if(rnd1==1){
TextView quesAns1 = (TextView) findViewById(R.id.answer1);
quesAns1.setText("1) " + answers[0]) ;
TextView quesAns2 = (TextView) findViewById(R.id.answer2);
quesAns2.setText("2) " + answers[1]) ;
TextView quesAns3 = (TextView) findViewById(R.id.answer3);
quesAns3.setText("3) " + answers[2]) ;
TextView quesAns4 = (TextView) findViewById(R.id.answer4);
quesAns4.setText("4) " + answers[3]) ;
}
if(rnd1==2){
TextView quesAns1 = (TextView) findViewById(R.id.answer1);
quesAns1.setText("1) " + answers[2]) ;
TextView quesAns2 = (TextView) findViewById(R.id.answer2);
quesAns2.setText("2) " + answers[0]) ;
TextView quesAns3 = (TextView) findViewById(R.id.answer3);
quesAns3.setText("3) " + answers[1]) ;
TextView quesAns4 = (TextView) findViewById(R.id.answer4);
quesAns4.setText("4) " + answers[3]) ;
}
if(rnd1==3){
TextView quesAns1 = (TextView) findViewById(R.id.answer1);
quesAns1.setText("1) " + answers[1]) ;
TextView quesAns2 = (TextView) findViewById(R.id.answer2);
quesAns2.setText("2) " + answers[2]) ;
TextView quesAns3 = (TextView) findViewById(R.id.answer3);
quesAns3.setText("3) " + answers[0]) ;
TextView quesAns4 = (TextView) findViewById(R.id.answer4);
quesAns4.setText("4) " + answers[3]) ;
}
}
});
//Answer 1 click functions
ans1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
ans1Action();
}
private void ans1Action() {
//enable_disable(0);
if(rnd1==1){
tick.setVisibility(View.VISIBLE);
}else{
cross.setVisibility(View.VISIBLE);
}
}
});
//Answer 2 click functions
ans2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
ans2Action();
}
private void ans2Action() {
//enable_disable(0);
if(rnd1==2){
tick.setVisibility(View.VISIBLE);
}else{
cross.setVisibility(View.VISIBLE);
}
}
});
//Answer 3 click functions
ans3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
ans3Action();
}
private void ans3Action() {
//enable_disable(0);
if(rnd1==3){
tick.setVisibility(View.VISIBLE);
}else{
cross.setVisibility(View.VISIBLE);
}
}
});
//Answer 4 click functions
ans4.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
ans4Action();
}
private void ans4Action() {
//enable_disable(0);
if(rnd1==4){
tick.setVisibility(View.VISIBLE);
}else{
cross.setVisibility(View.VISIBLE);
}
}
});
}
}
This :
quesAns4.setText("4) " + answers[0][3]);
Works if this is the 1st question.
What you should do is to have in the change_question function the number of the question in parameter change_question(int questionNumber)
Then, when you set the texts, you use :
quesAns4.setText("4) " + answers[questionNumber][3]);
If your questions start at 0.
Else, you use :
quesAns4.setText("1) " + answers[questionNumber-1][0]);
quesAns4.setText("2) " + answers[questionNumber-1][1]);
quesAns4.setText("3) " + answers[questionNumber-1][2]);
quesAns4.setText("4) " + answers[questionNumber-1][3]);
To print arrays in a user-friendly manner, you need to loop over the array items or you can use the Arrays.deepToString method:
String yourPrinterFriendlyArray = Arrays.deepToString(answers[0]);
quesAns4.setText("4) " + yourPrinterFriendlyArray);