我需要调用onActivityResult()
中定义的变量,并在main方法中检索它的值。这是我的代码
@Override
protected void onStart() {
final int questionsToAsk = 5;
super.onStart();
boolean[] questionsDone;
final Intent nextLevelScreen = getIntent();
final int[] questionsOrder = nextLevelScreen.getIntArrayExtra("questionsOrder");
final int[] currentIterator = {nextLevelScreen.getIntExtra("currIterator", 0)};
final int[] currentQuest = {questionsOrder[currentIterator[0]]};
setContentView(R.layout.activity_nextlevel_screen);
questionsDone = nextLevelScreen.getBooleanArrayExtra("questionsDONE");
ProgressBar barOfProgress = (ProgressBar) findViewById(R.id.barToShowProgress);
barOfProgress.setMax(questionsToAsk);
barOfProgress.setProgress(currentIterator[0]);
ImageButton btnToNextQuestion = (ImageButton) findViewById(R.id.buttonToNextQuestion);
final boolean[] finalQuestionsDone = questionsDone;
if (currentIterator[0] > questionsToAsk - 1) {
btnToNextQuestion.setImageResource(R.drawable.botaoproximatela);
} else {
btnToNextQuestion.setImageResource(R.drawable.botaoproximonivel);
}
btnToNextQuestion.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if (currentIterator[0] > questionsToAsk - 1) {
System.out.println("Game Over");
boolean[] correctAnswers = new boolean[5];
Intent answersIntent = getIntent();
for (int n = 0; n<5; n++) {
int currQuest = currentQuest[0];
switch (currQuest) {
case 0:
correctAnswers[n] = answersIntent.getBooleanExtra("isOneCorrect", false);
break;
case 1:
correctAnswers[n] = answersIntent.getBooleanExtra("isTwoCorrect", false);
break;
case 2:
correctAnswers[n] = answersIntent.getBooleanExtra("isThreeCorrect", false);
break;
case 3:
correctAnswers[n] = answersIntent.getBooleanExtra("isFourCorrect", false);
break;
case 4:
correctAnswers[n] = answersIntent.getBooleanExtra("isFiveCorrect", false);
break;
case 5:
correctAnswers[n] = answersIntent.getBooleanExtra("isSixCorrect", false);
break;
case 6:
correctAnswers[n] = answersIntent.getBooleanExtra("isSevenCorrect", false);
break;
case 7:
correctAnswers[n] = answersIntent.getBooleanExtra("isEightCorrect", false);
break;
case 8:
correctAnswers[n] = answersIntent.getBooleanExtra("isNineCorrect", false);
break;
case 9:
correctAnswers[n] = answersIntent.getBooleanExtra("isTenCorrect", false);
break;
}
}
System.out.println(Arrays.toString(correctAnswers));
setContentView(R.layout.activity_endgame_screen);
ImageButton returnbtn = (ImageButton) findViewById(R.id.btnToReturn);
returnbtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
finish();
}
});
} else {
Intent nextQuestionToAsk = null;
switch (questionsOrder[currentIterator[0]]) {
case 0:
nextQuestionToAsk = new Intent(getApplicationContext(), Quest1.class);
break;
case 1:
nextQuestionToAsk = new Intent(getApplicationContext(), Quest2.class);
break;
case 2:
nextQuestionToAsk = new Intent(getApplicationContext(), Quest3.class);
break;
case 3:
nextQuestionToAsk = new Intent(getApplicationContext(), Quest4.class);
break;
case 4:
nextQuestionToAsk = new Intent(getApplicationContext(), Quest5.class);
break;
case 5:
nextQuestionToAsk = new Intent(getApplicationContext(), Quest6.class);
break;
case 6:
nextQuestionToAsk = new Intent(getApplicationContext(), Quest7.class);
break;
case 7:
nextQuestionToAsk = new Intent(getApplicationContext(), Quest8.class);
break;
case 8:
nextQuestionToAsk = new Intent(getApplicationContext(), Quest9.class);
break;
case 9:
nextQuestionToAsk = new Intent(getApplicationContext(), Quest10.class);
break;
default:
System.out.println("Out of bounds");
break;
}
final Intent finalNextQuestionToAsk = nextQuestionToAsk;
if (finalNextQuestionToAsk != null) {
currentIterator[0]++;
System.out.println("curr It" + currentIterator[0]);
nextLevelScreen.putExtra("currIterator", currentIterator[0]);
finalQuestionsDone[questionsOrder[currentIterator[0] - 1]] = true;
nextLevelScreen.putExtra("questionsDONE", finalQuestionsDone);
startActivityForResult(finalNextQuestionToAsk, currentQuest[0]);
}
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Intent answersIntent = getIntent();
super.onActivityResult(requestCode, resultCode, data);
System.out.println("Received Result");
if (resultCode == RESULT_OK) {
switch (requestCode){
case 0:
final boolean oneIsCorrect = data.getBooleanExtra("oneIsCorrect", false);
if (oneIsCorrect) {
System.out.println("Correct");
Toast.makeText(getApplicationContext(), "Correto :)", Toast.LENGTH_SHORT).show();
answersIntent.putExtra("isOneCorrect", true);
} else {
System.out.println("Wrong");
Toast.makeText(getApplicationContext(), "Errado", Toast.LENGTH_SHORT).show();
}
break;
case 1:
final boolean twoIsCorrect = data.getBooleanExtra("twoIsCorrect", false);
if (twoIsCorrect) {
System.out.println("Correct");
Toast.makeText(getApplicationContext(), "Correto :)", Toast.LENGTH_SHORT).show();
answersIntent.putExtra("isTwoCorrect", true);
} else {
System.out.println("Wrong");
Toast.makeText(getApplicationContext(), "Errado", Toast.LENGTH_SHORT).show();
}
break;
case 2:
final boolean threeIsCorrect = data.getBooleanExtra("threeIsCorrect", false);
if (threeIsCorrect) {
System.out.println("Correct");
Toast.makeText(getApplicationContext(), "Correto :)", Toast.LENGTH_SHORT).show();
answersIntent.putExtra("isThreeCorrect", true);
} else {
System.out.println("Wrong");
Toast.makeText(getApplicationContext(), "Errado", Toast.LENGTH_SHORT).show();
}
break;
case 3:
final boolean fourIsCorrect = data.getBooleanExtra("fourIsCorrect", false);
if (fourIsCorrect) {
System.out.println("Correct");
Toast.makeText(getApplicationContext(), "Correto :)", Toast.LENGTH_SHORT).show();
answersIntent.putExtra("isFourCorrect", true);
} else {
System.out.println("Wrong");
Toast.makeText(getApplicationContext(), "Errado", Toast.LENGTH_SHORT).show();
}
break;
case 4:
final boolean fiveIsCorrect = data.getBooleanExtra("fiveIsCorrect", false);
if (fiveIsCorrect) {
System.out.println("Correct");
Toast.makeText(getApplicationContext(), "Correto :)", Toast.LENGTH_SHORT).show();
answersIntent.putExtra("isFiveCorrect", true);
} else {
System.out.println("Wrong");
Toast.makeText(getApplicationContext(), "Errado", Toast.LENGTH_SHORT).show();
}
break;
case 5:
final boolean sixIsCorrect = data.getBooleanExtra("sixIsCorrect", false);
if (sixIsCorrect) {
System.out.println("Correct");
Toast.makeText(getApplicationContext(), "Correto :)", Toast.LENGTH_SHORT).show();
answersIntent.putExtra("isSixCorrect", true);
} else {
System.out.println("Wrong");
Toast.makeText(getApplicationContext(), "Errado", Toast.LENGTH_SHORT).show();
}
break;
case 6:
final boolean sevenIsCorrect = data.getBooleanExtra("sevenIsCorrect", false);
if (sevenIsCorrect) {
System.out.println("Correct");
Toast.makeText(getApplicationContext(), "Correto :)", Toast.LENGTH_SHORT).show();
answersIntent.putExtra("isSevenCorrect", true);
} else {
System.out.println("Wrong");
Toast.makeText(getApplicationContext(), "Errado", Toast.LENGTH_SHORT).show();
}
break;
case 7:
final boolean eightIsCorrect = data.getBooleanExtra("eightIsCorrect", false);
if (eightIsCorrect) {
System.out.println("Correct");
Toast.makeText(getApplicationContext(), "Correto :)", Toast.LENGTH_SHORT).show();
answersIntent.putExtra("isEightCorrect", true);
} else {
System.out.println("Wrong");
Toast.makeText(getApplicationContext(), "Errado", Toast.LENGTH_SHORT).show();
}
break;
case 8:
final boolean nineIsCorrect = data.getBooleanExtra("nineIsCorrect", false);
if (nineIsCorrect) {
System.out.println("Correct");
Toast.makeText(getApplicationContext(), "Correto :)", Toast.LENGTH_SHORT).show();
answersIntent.putExtra("isNineCorrect", true);
} else {
System.out.println("Wrong");
Toast.makeText(getApplicationContext(), "Errado", Toast.LENGTH_SHORT).show();
}
break;
case 9:
final boolean tenIsCorrect = data.getBooleanExtra("tenIsCorrect", false);
if (tenIsCorrect) {
System.out.println("Correct");
Toast.makeText(getApplicationContext(), "Correto :)", Toast.LENGTH_SHORT).show();
answersIntent.putExtra("isTenCorrect", true);
} else {
System.out.println("Wrong");
Toast.makeText(getApplicationContext(), "Errado", Toast.LENGTH_SHORT).show();
}
break;
default:
System.out.println("OUTOFBOUNDS_code=" + requestCode);
break;
}
}
}
我尝试使用Intent存储所有值,但是当我用System.out.println(Arrays.toString(correctAnswers));
调用它们时,在设置值后,我得到一个完整的false数组。
怎么了?提前致谢
答案 0 :(得分:0)
onActivityResult
。
如果在发生这种情况时需要设置变量或执行方法,请从onActivityResult
内部进行。
oncreate {
...
}
public void doWhatYouNeedHere() {
...
}
protected void onActivityResult() {
...
doWhatYouNeedHere()
...
}
或许你可以设置一个监听器。