incrementing a value upon radio button click between 5 activities, 5th activity shows total of correct answers

StackOverflow https://stackoverflow.com/questions/22625059

Question

Hello i am trying to make an application where the user chooses an answer by radiobuttons. there are 5 questions if a radiobutton is clicked the second activity is started with a new question, and goes on until the last question. How do i set the total score of correct answers on the last activity?

here's my codes: (pls i really need help im beginner in android)

there are 5 question this 1st one..

package com.example.kei;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

    public static int counter=0;

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


    }



    public void choice_a(View view){
        Intent intent = new Intent(this, Page_two.class);
        startActivity(intent);
    }

    public void choice_b(View view){
        counter++;
        Intent intent = new Intent(this, Page_two.class);
        startActivity(intent);


    }

    public void choice_c(View view){
        Intent intent = new Intent(this, Page_two.class);
        startActivity(intent);
    }

}

last page:

package com.example.kei;



import android.os.Bundle;
import android.app.Activity;

import android.widget.TextView;

public class Total extends Activity {

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

        totalScore.setText(MainActivity.counter);
    }



}

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">kei</string>
    <string name="action_settings">Settings</string>
    <string name="total">Total: </string>
    <string name="score"> 0 </string>
    <string name="outOf"> /5 </string>


    <!-- QUESTION number1 -->
    <string name="question1">1.) The company that spearheaded the development of android operating system.</string>
    <string name="q1_choice_a">Microsoft</string>
    <string name="q1_choice_b">Google</string>
    <string name="q1_choice_c">Apple</string>

    <!-- QUESTION number2 -->
    <string name="question2">2.) The first publicly available smartphone running the Android OS.</string>
    <string name="q2_choice_a">Samsung Galaxy</string>
    <string name="q2_choice_b">Google Nexus</string>
    <string name="q2_choice_c">HTC Dream</string>

    <!-- QUESTION number3 -->
    <string name="question3">3.) The virtual machine which enables installation and running of application in android device.</string>
    <string name="q3_choice_a">Dalvik VM</string>
    <string name="q3_choice_b">Java Runtime Environment</string>
    <string name="q3_choice_c">.Net Framework</string>

    <!-- QUESTION number4 -->
    <string name="question4">4.) OHA is a consortion of hardware, software and telecom companies devoted to advancing open standards for mobile devices. What does the acronym  OHA stands for?</string>
    <string name="q4_choice_a">Overseas Housing Allowance</string>
    <string name="q4_choice_b">Ontario Hockey Asssociation</string>
    <string name="q4_choice_c">Open Handset Alliance</string>

    <!-- QUESTION number5 -->
    <string name="question5">5.) Android share of the global smartphone market as of 3rd qtr, 2013.</string>
    <string name="q5_choice_a">40%</string>
    <string name="q5_choice_b">58%</string>
    <string name="q5_choice_c">81%</string>
    <string name="title_activity_page_two">Page_two</string>
    <string name="title_activity_page_three">Page_three</string>
    <string name="hello_world">Hello world!</string>
    <string name="title_activity_page_four">Page_four</string>
    <string name="title_activity_page_five">Page_five</string>
    <string name="title_activity_total">Total</string>

</resources>
Was it helpful?

Solution

Create a AppSingelton Class as below:

public class AppSingleton {

    public static int score= 0;
}

now in each Activity increment its value as below:

if(optionCorrectSeleceted()){
AppSingelton.score=AppSingelton.score+5;
}

where optionCorrectSelected will be your method whose return type should be true if answer is correct else false. And in your final activity where you want to show the score just use...txtView.setText("Score :"+AppSingelton.score);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top