Click here to Skip to main content
15,888,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Can anybody help me. I have developed a quiz app. At the end of the quiz the result activity must show up but the scores activity shows up. The scores activity only shows up when the scores button is selected on the MainActivity.

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);
    }
}
Posted
Comments
Richard MacCutchan 8-Sep-15 10:56am    
And you want us to guess what happens?
Martin Park 8-Sep-15 11:24am    
the scores activity appears. But I want the result activity to appear

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