Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Can you help me. I have developed a app with a quiz. When the quiz is finished a new activity (result) appears showing the score. I want to pass the score to a new activity namely scores. Here is my code. What is wrong. The score must be showed in a textview.
Java
Result.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Result extends Activity
{

    private static Button playbtn;
    private static Button menubutton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        OnClickPlayButtonListener();
        OnClickMenuButtonListener();
        TextView textResult = (TextView) findViewById(R.id.textResult);
        Bundle b = getIntent().getExtras();
        int score = b.getInt("score");
        textResult.setText("You scored" + " " + score + " for the quiz.");
        Intent i = new Intent(getApplicationContext(), Result.class);
        i.putExtra("somevariable",score);
        startActivity(i);
    }

    public void OnClickPlayButtonListener() {
        playbtn = (Button) findViewById(R.id.btn);
        playbtn.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Quiz");
                        startActivity(intent);
                    }
                }
        );
    }

    public void OnClickMenuButtonListener() {
        menubutton = (Button) findViewById(R.id.menubtn);
        menubutton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                      Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                        startActivity(intent);
                      }
                }
        );


Scores.java
Java
import android.view.MenuItem;
import android.widget.TextView;
import android.content.Intent;

public class Scores extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scores);
        Intent intent = getIntent();
        String value = intent.getStringExtra("somevariable");
        TextView txtScore1 = (TextView) findViewById(R.id.txtScore1);
        txtScore1.setText("You scored" + " " + value + " for the quiz.");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_scores, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
Posted
Updated 5-Sep-15 23:14pm
v2

Instead of Result.class:
Intent i = new Intent(getApplicationContext(), Result.class);
'
shouldn't it be:
Intent i = new Intent(getApplicationContext(), Scores.class);
 
Share this answer
 
Comments
Martin Park 6-Sep-15 13:07pm    
Thanks. Can you maybe tell me why it shows that I have scored null for the quiz but for instance with the one try I got four correct?
Peter Leow 6-Sep-15 23:08pm    
In your original code, you were calling the Result class and in that result class, you were trying to set a textview with a non-existence key of "score", that why you got null.
As regards to why it is 4, it was the outcome of the logic of your other class that deals with deriving that score, you should check its correctness.
Martin Park 7-Sep-15 4:44am    
Thanks. Got it right. Only problem I have now is that the Scores activity shows up after the quiz and not the Result activity. I will paste the code below. Can you maybe help me on this please
Quiz.java
Java
package app.mobiledevicesecurity;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Quiz extends Activity
{
    List<question> questionList;
    int score = 0;
    int qid = 0;
    Question currentQuest;
    TextView txtQuestion, scored;
    Button button1, button2, button3;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);
        QuizHelper db = new QuizHelper(this);
        questionList = db.getAllQuestions();
        currentQuest = questionList.get(qid);
        txtQuestion = (TextView) findViewById(R.id.txtQuestion);

        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);

        scored = (TextView) findViewById(R.id.score);



        setQuestionView();

        button1.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
               getAnswer(button1.getText().toString());
            }
        });

        button2.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                getAnswer(button2.getText().toString());
            }
        });

        button3.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                getAnswer(button3.getText().toString());
            }
        });
    }
    public void getAnswer(String AnswerString)
    {
        if (currentQuest.getAnswer().equals(AnswerString))
        {
            score++;
            scored.setText("Score : " + score);
        }
        else
        {

            Intent intent = new Intent(Quiz.this,
                    Result.class);
            Bundle b = new Bundle();
            b.putInt("score", score);
            intent.putExtras(b);
            startActivity(intent);
            finish();


        }
        if (qid < questionList.size()) {
            currentQuest = questionList.get(qid);
            setQuestionView();
        }
        else
        {

            Intent intent = new Intent(Quiz.this,
                    Result.class);
            Bundle b = new Bundle();
            b.putInt("score", score);
            intent.putExtras(b);
            startActivity(intent);
            finish();


        }
    }

    private void setQuestionView()
    {
        txtQuestion.setText(currentQuest.getQuest());
        button1.setText(currentQuest.getOption1());
        button2.setText(currentQuest.getOption2());
        button3.setText(currentQuest.getOption3());
        qid++;
    }
}


Result.java
Java
package app.mobiledevicesecurity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Result extends Activity {

    private static Button playbtn;
    private static Button menubutton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        OnClickPlayButtonListener();
        OnClickMenuButtonListener();
        TextView textResult = (TextView) findViewById(R.id.textResult);
        Bundle b = getIntent().getExtras();
        int score = b.getInt("score");
        textResult.setText("You scored" + " " + score + " for the quiz.");

        Intent intent2 = new Intent(Result.this,
               Scores.class);
        Bundle bun = new Bundle();
        bun.putInt("score", score);
        intent2.putExtras(bun);
        startActivity(intent2);
        finish();
    }

    public void OnClickPlayButtonListener() {
        playbtn = (Button) findViewById(R.id.btn);
        playbtn.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Quiz");
                        startActivity(intent);
                    }
                }
        );
    }

    public void OnClickMenuButtonListener() {
        menubutton = (Button) findViewById(R.id.menubtn);
        menubutton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                        startActivity(intent);
                    }
                }
        );
    }
}


Scores.java
Java
package app.mobiledevicesecurity;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;



import android.widget.TextView;
import android.content.Intent;

public class Scores extends ActionBarActivity {

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

        TextView txtScore1 = (TextView) findViewById(R.id.txtScore1);
        Bundle bun = getIntent().getExtras();
        int score = bun.getInt("score");
        txtScore1.setText("score:" + " " + score + " for the quiz.");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_scores, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}


Only problem I have now is that the Scores activity shows up after the quiz and not the Result activity. Anyone to help me please
 
Share this answer
 
v2
Using startActivityForResult and handler callback by Override onActivityResult
 
Share this answer
 
Comments
Martin Park 7-Sep-15 13:07pm    
Can you maybe help me with the code. I am new to this. If I see the code I will maybe understand. Thank you
Ngo Tuong Dan 8-Sep-15 23:00pm    
https://drive.google.com/folderview?id=0B6Hrf3I5GN_PfmR5ckdKd3NScjdtSU16SGFOcEdSUHBIUE5oV0ZZQXMyTnJ0TzI1V09IREE&usp=sharing

you can see this demo (this demo for my student before)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900