我写了一个代码,用户必须回答问题的真假。我想要做的是每当用户正确回答时我会向我的arraylist添加+1。然后在游戏结束时我会显示已回答问题的数量。我试着按照自己的方式去做,但是我收到了错误。
以下是我创建列表的方法:
final ArrayList<Integer> points = new ArrayList<Integer>();
这是按钮,如果用户按下True(为了更好地理解:当用户按下true时,弹出窗口打开,答案正确或错误以及问题的正确答案。当用户点击closepopup按钮时如果方法运行并检查他是否按下了右键,如果不是最后一个问题。如果没有,则显示下一个问题并将+1添加到列表中):
mYes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
initiatePopupWindow();
if(type.get(count[0])){
((TextView)pwindo.getContentView().findViewById(R.id.popupTekstasTiesaArNe)).setText("Correct!");
} else {
((TextView)pwindo.getContentView().findViewById(R.id.popupTekstasTiesaArNe)).setText("False!");
}
//Show the first answer on first button click.
((TextView)pwindo.getContentView().findViewById(R.id.popupTekstas)).setText(questions.get((count[0]) % questions.size()).answer);
// When PopUp button closes open the next question with the if/else conditions.
btnClosePopup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//if the question is true show next question/ else close app
if (type.get(count[0])) {
points.add(1); // if the answer is correct add +1 to the list.
if(questions.size()-1 == count[0]) // if you count[0] is init to 0
{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("WInner");
builder.setMessage("You won, play again?");
builder.setCancelable(false);
builder.setPositiveButton(android.R.string.yes,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// just close dialog
dialog.cancel();
}
});
builder.setNegativeButton(android.R.string.no,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
mResult.setText(points.size());// here I try to print the number of answered questions.
}
});
// Create dialog from builder
AlertDialog alert = builder.create();
// Show dialog
alert.show();
count[0]=0;
}
else if(questions.size()-1 < count[0])
try {
throw new Exception("Invalid ");
} catch (Exception e) {
e.printStackTrace();
}
else
count[0]++;
mQuestion.setText(questions.get(count[0]).question); // you dont need calculate the module anymore
pwindo.dismiss();
} else {
count[0]++;
mQuestion.setText(questions.get(count[0]).question); // you dont need calculate the module anymore
pwindo.dismiss();
}
}
});
}
});
EDITED !!!
我弄清楚为什么SetText会显示空白的文本视图。我想在问题出现前在活动中显示已回答的问题。我有一个menuActivity&gt; LevelselectActivity&gt; GameActivity。在游戏活动中,我添加到列表值。如何在LevelselectActivity中发布它们?我应该使用共享偏好吗?我现在不知道..