带有SQLite的多项选择测验应用(分数未在新活动中显示)

时间:2018-08-21 02:55:49

标签: java android sqlite

我是android的新手,我只是通过观看带有sqlite集成系列(Youtube播放列表)的多项选择测验应用来做一个android应用。现在我有一个问题。在本教程中,他们有2个活动(主要活动测验活动),QuizActivity的得分将发送回MainActivity。但是我有3个活动(主要活动 QuestionActivity ResultActivity )。我想将分数从QuestionActivity发送到ResultActivity,并用textView显示它。问题是分数总是显示为0,但是当我调试showResult()时,分数显示为调试结果。请检查代码并为我解决。 android真的很新,所以请帮助我。

  

这是调试结果。

Debug result

MainActivity

package com.enkoala.enkoala;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

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

    Button buttonGeneral= findViewById(R.id.button_general_english);
    buttonGeneral.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            GeneralEnglish();
        }
    });
}
private void GeneralEnglish(){
    Intent intent=new Intent(MainActivity.this,QuestionActivity.class);
    startActivity(intent);
}
}

QuestionActivity

package com.enkoala.enkoala;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Collections;
import java.util.List;


public class QuestionActivity extends AppCompatActivity {
public   static final int REQUEST_CODE_QUIZ = 1;

public static final String EXTRA_SCORE="extra_score";


private TextView textViewQuestion;
private TextView textViewQuestionCount;
private RadioGroup radioGroup;
private RadioButton radioButton1;
private RadioButton radioButton2;
private RadioButton radioButton3;
private Button buttonNext;

private ColorStateList textColorDefaultRb;

private List<Question> questionList;

private int questionCounter;
private int questionCountTotal;
private Question currentQuestion;

private int score;
private boolean answered;

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

    textViewQuestion= findViewById(R.id.text_view_question);
    textViewQuestionCount= findViewById(R.id.text_view_question_count);
    radioGroup= findViewById(R.id.radio_group);
    radioButton1= findViewById(R.id.radio_button1);
    radioButton2= findViewById(R.id.radio_button2);
    radioButton3= findViewById(R.id.radio_button3);
    buttonNext= findViewById(R.id.button_next);

    QuizDatabseHelper databseHelper=new QuizDatabseHelper(this);
    questionList=databseHelper.getAllQuestions();
    questionCountTotal=questionList.size();
    Collections.shuffle(questionList);

    showNextQuestion();

    buttonNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (!answered){
                if (radioButton1.isChecked() || radioButton2.isChecked() || radioButton3.isChecked()){
                    checkAnswer();
                }else {
                    Toast.makeText(QuestionActivity.this, "Please select an answer",                                                     Toast.LENGTH_SHORT).show();
                }
            }else {
                showNextQuestion();
            }
        }
    });
    }
    private void showNextQuestion(){
    radioGroup.clearCheck();

    if (questionCounter<questionCountTotal){
        currentQuestion=questionList.get(questionCounter);
        textViewQuestion.setText(currentQuestion.getQuestion());
        radioButton1.setText(currentQuestion.getOption1());
        radioButton2.setText(currentQuestion.getOption2());
        radioButton3.setText(currentQuestion.getOption3());

        questionCounter++;
        textViewQuestionCount.setText("Question: "+ questionCounter + "/" + questionCountTotal);
        answered=false;
        buttonNext.setText("Next");
    }else {
        buttonNext.setText("Get Result");
        showResult();
    }
    }
    public void showResult(){
        Intent resultIntent = new Intent(QuestionActivity.this,ResultActivity.class);
        resultIntent.putExtra(EXTRA_SCORE,score);
        setResult(RESULT_OK,resultIntent);
        startActivityForResult(resultIntent,REQUEST_CODE_QUIZ);
    }


    private void checkAnswer(){
    answered=true;

    RadioButton radioButtonselected=findViewById(radioGroup.getCheckedRadioButtonId());
    int answernumber = radioGroup.indexOfChild(radioButtonselected) + 1;
    if (answernumber == currentQuestion.getAnswernumber()){
        score++;
    }
    showNextQuestion();
    }
}

ResultActivity

package com.enkoala.enkoala;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import static com.enkoala.enkoala.QuestionActivity.REQUEST_CODE_QUIZ;

public class ResultActivity extends AppCompatActivity {

public static final String SHARED_PREFS = "sharedPrefs";
public static final String KEY_HIGHSCORE = "keyhighscore";
private TextView textViewHighscore;
private int highscore;


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

    textViewHighscore = findViewById(R.id.text_view_score);
    loadHighscore();

    final Button buttonback = findViewById(R.id.button_back);
    buttonback.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            buttonBack();
        }
    });

}

private void buttonBack() {
    Intent intent = new Intent(ResultActivity.this, MainActivity.class);
    startActivity(intent);
}

protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == REQUEST_CODE_QUIZ) {
        if (resultCode == RESULT_OK) {
            int score = data.getIntExtra(QuestionActivity.EXTRA_SCORE, 0);
            if (score > highscore) {
                updateHighscore(score);
            }
        }
    }
}

private void loadHighscore() {
    SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
    highscore = sharedPreferences.getInt(KEY_HIGHSCORE, 0);
    textViewHighscore.setText("Your score is " + highscore + " out of 25. ");
}

private void updateHighscore(int highscoreNew) {
    highscore = highscoreNew;
    textViewHighscore.setText("Your score is " + highscore + " out of 25. ");
    SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt(KEY_HIGHSCORE, highscore);
    editor.apply();
}

}

1 个答案:

答案 0 :(得分:0)

您是否要调试结果活动?乍一看,您的代码就可以了。但是我认为您的onActivityResult()方法可能有问题,因为我知道这是一个异步任务。因此,您在收到loadHighscore()的值后,应该在onActivityResult()中转移score。认为它应该有所帮助。