GenEdQuestion.class The mode of quiz is multiple choice. I want to save or transfer the correct answer and the chosen answer to another activity (result). I transferred the score but i don't know how to transfer the result. How can I do that?
answer1.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (answer1.getText() == mAnswer){
mNumber++;
mScore++;
number.setText(" "+mNumber+" .");
if (mNumber == quizCount){
onFinish();
}
else{
updateQuestion(r.nextInt(mQuestionsLength));
}
}else{
mNumber++;
number.setText(" "+mNumber+" .");
if (mNumber == quizCount){
onFinish();
}
else{
updateQuestion(r.nextInt(mQuestionsLength));
}
}
return false;
}
});
answer2.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (answer2.getText() == mAnswer){
mNumber++;
number.setText(" "+mNumber+" .");
mScore++;
if (mNumber == quizCount){
onFinish();
}
else{
updateQuestion(r.nextInt(mQuestionsLength));
}
}else{
mNumber++;
number.setText(" "+mNumber+" .");
if (mNumber == quizCount){
onFinish();
}
else{
updateQuestion(r.nextInt(mQuestionsLength));
}
}
return false;
}
});
answer3.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (answer3.getText() == mAnswer){
mNumber++;
number.setText(" "+mNumber+" .");
mScore++;
if (mNumber == quizCount){
onFinish();
}
else{
updateQuestion(r.nextInt(mQuestionsLength));
}
}else{
mNumber++;
number.setText(" "+mNumber+" .");
if (mNumber == quizCount){
onFinish();
}
else{
updateQuestion(r.nextInt(mQuestionsLength));
}
}
return false;
}
});
answer4.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (answer4.getText() == mAnswer){
mNumber++;
number.setText(" "+mNumber+" .");
mScore++;
if (mNumber == quizCount){
onFinish();
}
else{
updateQuestion(r.nextInt(mQuestionsLength));
}
}else{
mNumber++;
number.setText(" "+mNumber+" .");
if (mNumber == quizCount){
onFinish();
}
else{
updateQuestion(r.nextInt(mQuestionsLength));
}
}
return false;
}
});
new CountDownTimer(60000*60, 1000){
public void onTick(long secondsLeft){
mCountDownGenEd.setText(""+String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes(secondsLeft),
TimeUnit.MILLISECONDS.toSeconds(secondsLeft) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(secondsLeft))));
}
public void onFinish(){
Intent intent = new Intent(getApplicationContext(), GenEdQuestionsResult.class);
intent.putExtra("this", mScore);
startActivity(intent);
}
}.start();
}
private void onFinish() {
Intent intent = new Intent(getApplicationContext(), GenEdQuestionsResult.class);
intent.putExtra("this", mScore);
startActivity(intent);
}
private void updateQuestion(int i) {
question.setText(mQuestions.getQuestion(i));
answer1.setText(mQuestions.getChoice1(i));
answer2.setText(mQuestions.getChoice2(i));
answer3.setText(mQuestions.getChoice3(i));
answer4.setText(mQuestions.getChoice4(i));
mAnswer = mQuestions.getCorrectAnswer(i);
}
GenEdQuestionsResult.class This activity will show the score and if the user passed or failed. How will i put the result here?
mFinalScore = (TextView) findViewById(R.id.scoreTextView);
mResult = (TextView) findViewById(R.id.resultTextView);
int score = getIntent().getIntExtra("this", 0);
mFinalScore.setText(score+" / 20");
if (score <= 10){mResult.setText("Failed");}
else{mResult.setText("Passed");}
This is the result i want to create
Three ways, shared preferences or getter and setter methods, or putextra when starting a new activity like below:
Shared preferences
Setting values in Preference:
// MY_PREFS_NAME - a static String variable like:
//public static final String MY_PREFS_NAME = "MyPrefsFile";
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("name", "Elena");
editor.putInt("idName", 12);
editor.apply();
Retrieve data from preference:
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
int idName = prefs.getInt("idName", 0); //0 is the default value.
}
Referenced to : https://stackoverflow.com/a/23024962/4674760
Getter/Setter in a new activity
String newenq="New Enquiry";
public void setNewenq(String newenq) {
this.newenq = newenq;
}
public String getNewenq() {
return newenq;
}
PutExtra
String easyPuzzle = "answer";
Intent i = new Intent(this, ToClass.class);
i.putExtra("epuzzle", easyPuzzle);
startActivity(i)
you have 3 approaches to do this the first one=> make a static variable and then you can access it where ever you want ,
the second one you can => use intent putExtra
Intent i = new Intent(this, SecondClass.class);
i.putExtra("answer", "myAnswer");
startActivity(i)
the third approach is using a sharedPreference take a look at this link https://developer.android.com/reference/android/content/SharedPreferences.html
Use shared preference like this:
public static final String answer = "answer"; //declare this globally at the top.
SharedPreferences prefs = getSharedPreferences(Commonclass.ANSWER,Context.MODE_PRIVATE);
SharedPreferences.Editor editor;
editor = prefs.edit();
String a = prefs.getString("answer","answer"); //to get the stored value
editor.putString(theme,"blue"); // to store your answer
editor.commit();
Create a Commonclass:
public class Commonclass
{
public static final String ANSWER = "answer" ;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.