Skip to content
Advertisement

Modifying the scoring of a quiz app (android studio)

Context about the app:

This is a sample 5 question test app that I made while I also learned firebase database just yesterday. The app is working perfectly and it does it needs to do, BUT what I wanna do next is that there are no right answers and each button corresponds to a score

ex: Question: What are you feeling out of 1-5

A.)1 – I am feeling bad B.)2 – I am feeling sad C.)3 – neutral D.)4 – I am feeling happy E.)5 – I am feeling good

As you can see here if the user answers E for example then he gets 5 pts (the points is not shown) and the user will get 1 point if he answers A and so on…

code:

{
    private TextView mScoreView;
    private int questionNumber = 0;
    private TextView mQuestion, result;
    private Button choice1,choice2,choice3,choice4,quit;
    private int nScore = 0;
    private String ans;
    private Firebase question,c1,c2,c3,c4,answer;


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

        result = (TextView) findViewById(R.id.result);
        mScoreView = (TextView) findViewById(R.id.score);
        mQuestion = (TextView) findViewById(R.id.question);
        choice1 = (Button) findViewById(R.id.c1);
        choice2 = (Button) findViewById(R.id.c2);
        choice3 = (Button) findViewById(R.id.c3);
        choice4 = (Button) findViewById(R.id.c4);
        quit = (Button) findViewById(R.id.quit);


        updateQuestion();



        quit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Maths2.this,MainActivity.class);

                startActivity(intent);
                finish();

            }
        });






//CHOICE 1//////////////////////////////////////////////////////////////
        choice1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (choice1.getText().equals(ans)) {
                    nScore = nScore + 1;
                    updateScore(nScore);
                    updateQuestion();
                }else
                    {
                        updateQuestion();

                }

            }
        });
        //CHOICE 1 //////////////////////////////////////////////////////////

        //CHOICE 2//////////////////////////////////////////////////////////////
        choice2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (choice2.getText().equals(ans)) {
                    nScore = nScore + 2;
                    updateScore(nScore);
                    updateQuestion();
                }else
                {
                    updateQuestion();

                }

            }
        });
        //CHOICE 2 //////////////////////////////////////////////////////////

        //CHOICE 3//////////////////////////////////////////////////////////////
        choice3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (choice3.getText().equals(ans)) {
                    nScore = nScore + 3;
                    updateScore(nScore);
                    updateQuestion();
                }else
                {
                    updateQuestion();

                }

            }
        });
        //CHOICE 3 //////////////////////////////////////////////////////////

        //CHOICE 4//////////////////////////////////////////////////////////////
        choice4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (choice4.getText().equals(ans)) {
                    nScore = nScore + 4;
                    updateScore(nScore);
                    updateQuestion();
                }else
                {
                    updateQuestion();

                }

            }
        });
        //CHOICE 4 //////////////////////////////////////////////////////////



    }
    private void updateScore (int score){
       mScoreView.setText("" + nScore);
    }





    private void updateQuestion() {

        question = new Firebase("https://thesis-e4f56-default-rtdb.firebaseio.com/"+ 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) {

            }





        });
        c1= new Firebase("https://thesis-e4f56-default-rtdb.firebaseio.com/"+ 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/"+ 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) {

            }
        });


        c3= new Firebase("https://thesis-e4f56-default-rtdb.firebaseio.com/"+ questionNumber +"/choice3");
        c3.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String choice = dataSnapshot.getValue(String.class);
                choice3.setText(choice);
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });

        c4= new Firebase("https://thesis-e4f56-default-rtdb.firebaseio.com/"+ 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) {

            }
        });

        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 (nScore == 10) {

            result.setText("good job");

        }
        questionNumber ++;


    }
}

I am doing this since yesterday and I also just learned doing firebase yesterday. I just got stumped for the past hour on what the logic is.

Advertisement

Answer

You need to remove the condition choice4.getText().equals(ans) inside all the choice click listeners. You should also put the code in a common method.

public void handleChoiceClicked(){
 nScore = nScore + 2;
 updateScore(nScore);
 updateQuestion();
}

call it from the click listeners rather than duplicating them.

           choice1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    handleChoiceClicked();
                }
            });
            //CHOICE 1 //////////////////////////////////////////////////////////

            //CHOICE 2//////////////////////////////////////////////////////////////
            choice2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    handleChoiceClicked();
                }
            });
            //CHOICE 2 //////////////////////////////////////////////////////////

            //CHOICE 3//////////////////////////////////////////////////////////////
            choice3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    handleChoiceClicked();
                }
            });
            //CHOICE 3 //////////////////////////////////////////////////////////

            //CHOICE 4//////////////////////////////////////////////////////////////
            choice4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    handleChoiceClicked();
                }
            });
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement