0

Well, I'm new in Java programming, and following a few tips that I've seen around here, I made this code to a quiz game:

public class OtherActivity extends Activity {


    TextView textView1, textView2;
    Button btn1, btn2, btn3, btn4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other);

        textView1 = (TextView) findViewById(R.id.textView1);
        textView2 = (TextView) findViewById(R.id.textView2);

        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);


        final ArrayList<Question> qsts = new ArrayList<Question>();
        qsts.add(Question.q1);
        qsts.add(Question.q2);
        qsts.add(Question.q3);
        qsts.add(Question.q4);
        qsts.add(Question.q5);
        qsts.add(Question.q6);

        final Random rng = new Random();

        final List<Integer> generated = new ArrayList<Integer>();
        int nxt = rng.nextInt(6);

                    generated.add(nxt);

                    final Question nextQuestion = qsts.get(nxt);

                    textView1.setText(nextQuestion.questionText);

                    final ArrayList<String> allAnswers = new ArrayList<String>();
                    allAnswers.add(nextQuestion.correctAnswerText);
                    allAnswers.add(nextQuestion.wrongAnswer1);
                    allAnswers.add(nextQuestion.wrongAnswer2);
                    allAnswers.add(nextQuestion.wrongAnswer3);

                    Collections.shuffle(allAnswers);

                    btn1.setText(allAnswers.get(0));
                    btn2.setText(allAnswers.get(1));
                    btn3.setText(allAnswers.get(2));
                    btn4.setText(allAnswers.get(3));

                    btn1.setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {
                            if(allAnswers.get(0) == nextQuestion.correctAnswerText){
                                textView2.setText("CORRECT ANSWER, MAN!");

                                while(true){

                                    int nxt = rng.nextInt(6);

                                    if (!generated.contains(nxt)){

                                        generated.add(nxt);

                            Question nextQuestion = qsts.get(nxt);

                            textView1.setText(nextQuestion.questionText);

                            ArrayList<String> allAnswers = new ArrayList<String>();
                            allAnswers.add(nextQuestion.correctAnswerText);
                            allAnswers.add(nextQuestion.wrongAnswer1);
                            allAnswers.add(nextQuestion.wrongAnswer2);
                            allAnswers.add(nextQuestion.wrongAnswer3);

                            Collections.shuffle(allAnswers);

                            btn1.setText(allAnswers.get(0));
                            btn2.setText(allAnswers.get(1));
                            btn3.setText(allAnswers.get(2));
                            btn4.setText(allAnswers.get(3));

                                        break;
                                    }
                                }

                            }
                            else{
                                textView2.setText("WRONG ANSWER, MAN!");

                                while(true){

                                    int nxt = rng.nextInt(6);

                                    if (!generated.contains(nxt)){

                                        generated.add(nxt);

                            //  ---->   Integer nxt = rng.nextInt(6); <---- random nxt aqui!

                            Question nextQuestion = qsts.get(nxt);

                            textView1.setText(nextQuestion.questionText);

                            ArrayList<String> allAnswers = new ArrayList<String>();
                            allAnswers.add(nextQuestion.correctAnswerText);
                            allAnswers.add(nextQuestion.wrongAnswer1);
                            allAnswers.add(nextQuestion.wrongAnswer2);
                            allAnswers.add(nextQuestion.wrongAnswer3);

                            Collections.shuffle(allAnswers);

                            btn1.setText(allAnswers.get(0));
                            btn2.setText(allAnswers.get(1));
                            btn3.setText(allAnswers.get(2));
                            btn4.setText(allAnswers.get(3));

                                        break;
                                    }
                                }

                            }
                        }
                    });

                    //
                    // AND THE SAME METHOD TO THE BUTTONS btn2, btn3, btn4
                    //
                    // btn2 with a if (allAnswers1.get(1) == nextQuestion.correctAnswerText) { ...
                    // btn3 with a if (allAnswers1.get(2) == nextQuestion.correctAnswerText) { ...
                    // btn4 with a if (allAnswers1.get(3) == nextQuestion.correctAnswerText) { ...
                    //

    }
}

And I have this other class code:

    public class Question {

    String questionText;
    String correctAnswerText;       
    String wrongAnswer1;
    String wrongAnswer2;
    String wrongAnswer3;


    Question (String qst, String cAns, String wAns1, String wAns2, String wAns3){

        questionText = qst;
        correctAnswerText = cAns;
        wrongAnswer1 = wAns1;
        wrongAnswer2 = wAns2;
        wrongAnswer3 = wAns3;

    }

    static Question q1 = new Question(
            "Question 1",

            "Correct answer - question 1",
            "Wrong Answer 1 - question 1",
            "Wrong Answer 2 - question 1",
            "Wrong Answer 3 - question 1"
            );
    static Question q2 = new Question(
            "Question 2",

            "Correct answer - question 2",
            "Wrong Answer 1 - question 2",
            "Wrong Answer 2 - question 2",
            "Wrong Answer 3 - question 2"
            );
    // ...
    // and the same with q3, q4, q5 and q6
    // ...

Well, how you can see, I'm having problems with the identification of the correct answer. In the first question it works fine, but not after this. I believe this problem happens because when I try to compare allAnswers.get(0) == nextQuestion.correctAnswerText or with other index like (1), (2) or (3) of the ArrayList allAnswers, the "slot" selectioned for the comparation still have the first question text answer. So, what should I do?

*Plus: The code is getting really big, i tried to put an if in the onClickListener and compare with the id of the 4 buttons, but it had to be with other function, outside the onCreate, 'cause the ArrayList qsts (that holds the Question objects) is not declared and I can't compare the button clicked cause I must declare a different ArrayList. There's a better way to do what I want?

  • where you put your DB? From where Your `questions & answers` coming? – Rahul Patel Mar 08 '13 at 03:50
  • Do I need a database for this? I created the objects questions (q1, q2, q3...) in the class code, and it's all in the same package. In the activity, I can show the question text and the answers randomly in the buttons. The problem's the comparation. –  Mar 08 '13 at 04:00
  • suggest to use a database or a xml file. Having static variables is not recommended. May lead to out of memory error in some cases. – Raghunandan Mar 08 '13 at 04:35

4 Answers4

0

For problem of having large code i suggest u to make separate onclicklistener and put that button.setonclicklistener in all 4 buttons.

Then on click listener you got view as parameter, that view is a button that is pressed. cast it to button and then get text of button. then u can set if else leader that you implement in button action.

Make different method to set new question so that you have to just call it rather then writing same code multi times.

I hope it will resolve both of your problems.

vikrant kumar
  • 208
  • 2
  • 14
  • Yes Royce, if you make a database for your questions and ans then your code performance will improved and code size will reduce. – vikrant kumar Mar 08 '13 at 04:18
  • Ok, man. I made a method called `generateQuestion()`. But still when I try to compare `if(buttonText.equals(nextQuestion.correctAnswerText))` the eclipse shows me an error message because don't recongnize the object `nextQuestion`. There's some way to get the text of the object considering that it's not declared in the onClick method? –  Mar 08 '13 at 05:18
  • Ok dear you can set your object in button as tag (Button.settag(Your object)). which you can get your object as it is in onclicklistener((object)button.gettag()). then you can get all your data from that object. :-) – vikrant kumar Mar 08 '13 at 06:27
0

Follow the instruction as vikrant said.

And in addition, do put one boolean variable which value will be change while user select any options.

How by checking that boolean value you can able to get whether user has answered or not.

And to check for the corrent answer you will have to compare the selected value with the correct value.

Enjoy Coding. :)

Shreyash Mahajan
  • 23,386
  • 35
  • 116
  • 188
0

hear is the example what vikrant said

public class MyActivity extends Activity implements OnClickListener {

Question nextQuestion;

@Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_other);
        .......  code
   }
}

@Override
public void onClick(View v) {
Button b = (Button)v;
String buttonText = b.getText().toString();

   if(buttonText.equals( nextQuestion.correctAnswerText ))
   { 
      DoSomething();
      return;
   }else{

   }
}

And Dont forgot to add

Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener(this);
Chirag Patel
  • 11,416
  • 3
  • 26
  • 38
  • Ok, I understand that. I did it, but still not working 'cause the object nextQuestion is not recognized, it's declared in another method `generateQuestion()`. How can I compare those values if in the onClick method the program doesn't recognize the object's value? –  Mar 08 '13 at 05:24
  • I edited my answer as your requirement @SirRoyce please check it once – Chirag Patel Mar 08 '13 at 05:28
  • Man, can I send u the code? Just to u take a look and say me what I'm doing wrong, 'cause the app still with problems. –  Mar 08 '13 at 06:24
  • You need to learn How to explain your problem with others otherwise stack overflow is no use for you. You can not send code every time to other. First try once.. what type of error you got if dont understand share screen shots – Chirag Patel Mar 08 '13 at 06:27
  • The problem still in the comparation. I put the `Question nextQuestion;` before the onCreate. When the comparation happens, the `nextQuestion.correctAnswerText` is recognized, but the app stops. I tried to compare with something else and it worked. –  Mar 08 '13 at 06:57
  • and the Question.java file is fine. –  Mar 08 '13 at 06:58
  • print both `nextQuestion.correctAnswerText` and `buttonText` is it same ? – Chirag Patel Mar 08 '13 at 07:01
0

If you want to create Quiz application, then check this LINK.

also check this link for the use of existing database with an Android application

if you have any problem with that then please tell me.

Good Luck.

Community
  • 1
  • 1
Rahul Patel
  • 3,823
  • 5
  • 30
  • 46