我正在使用Dialog在Android中显示一系列测验。我想在用户回答正确或错误时显示下一个测验。
我正在使用自定义布局,我使用以下代码控制它:
public void showQuizDialog() {
View v;
TextView question;
// Textview showing Quiz 1 out of 3
TextView numQ;
NetworkImageView image;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
// The coordinator keeps track of the game
coordinator.setQuizStarted();
// Get the quiz using the coordinator
Quiz qX = coordinator.getQuiz();
final String answer = qX.getAnswer();
final String quizTracker = coordinator.getTracker();
// Create an instance of the dialog fragment
AlertDialog.Builder quiz = new AlertDialog.Builder(MyActivity.this);
LayoutInflater inflater = MyActivity.this.getLayoutInflater();
// Inflate and set the layout for the dialog
v = inflater.inflate(R.layout.quiz_layout,null);
// Set the question
question = (TextView) v.findViewById(R.id.content_quiz);
question.setText(qX.getQuestion());
// Set current number of question
numQ = (TextView) v.findViewById(R.id.number_quiz);
numQ.setText(quizTracker);
// Retrieves the image from url
image = (NetworkImageView) v.findViewById(R.id.thumbnail_quiz);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
String urlImage = getCompleteUrl(CODE_QUIZ_IMG);
urlImage += qX.getImg();
image.setImageUrl(urlImage, imageLoader);
quiz.setPositiveButton("True", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
if (answer.equals(quiz_true)) {
coordinator.notifyCorrectQuizNum();
Log.d(QUIZ_DIALOG,"\t\tCorrect answer! it was true");
}
coordinator.setQuizEnded(true);
coordinator.updateQuizLeft();
}
})
.setNegativeButton("False", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked to False btn
if (answer.equals(quiz_false)){
Log.d(QUIZ_DIALOG,"\t\tCorrect Answer!! It was false");
coordinator.notifyCorrectQuizNum();
}
coordinator.setQuizEnded(true);
coordinator.updateQuizLeft();
}
});
quiz.setView(v);
final AlertDialog dialog = quiz.create();
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
假设要显示100个测验,并且还有一个Coordinator类的方法,它返回剩余测验的数量。
所以,通话后
coordinator.setQuizEnded(true);
coordinator.updateQuizLeft();
coordinator.getQuizLeft()
将返回99. coordinator.getQuiz()
将指向下一个测验。
如何在不关闭对话框的情况下更新TextView question
和numQ
?
答案 0 :(得分:3)
尝试这样的事情:
首先调用setupQuizDialog()并创建一个没有任何内容的对话框。loadNewQuestionIntoDialog()被连续调用,用新问题刷新对话框的内容。
public void setupQuizDialog() {
LayoutInflater inflater = MyActivity.this.getLayoutInflater();
// Inflate and set the layout for the dialog
View v = inflater.inflate(R.layout.quiz_layout,null);
AlertDialog.Builder quiz = new AlertDialog.Builder(MyActivity.this)
.setPositiveButton("True", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Nothing, will replace later
}
})
.setNegativeButton("False", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Nothing, will replace later
}
});
quiz.setView(v);
final AlertDialog dialog = quiz.create();
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
loadNewQuestionIntoDialog(dialog, 100);
}
public void loadNewQuestionIntoDialog(final AlertDialog dialog, final int questionNumber){
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
// The coordinator keeps track of the game
coordinator.setQuizStarted();
// Get the quiz using the coordinator
final String answer = qX.getAnswer();
final String quizTracker = coordinator.getTracker();
TextView question = (TextView) dialog.findViewById(R.id.content_quiz);
question.setText(qX.getQuestion());
TextView numQ = (TextView) dialog.findViewById(R.id.number_quiz);
numQ.setText(quizTracker);
// Retrieves the image from url
NetworkImageView image = (NetworkImageView) dialog.findViewById(R.id.thumbnail_quiz);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
String urlImage = getCompleteUrl(CODE_QUIZ_IMG);
urlImage += qX.getImg();
image.setImageUrl(urlImage, imageLoader);
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// User clicked OK button
if (answer.equals(quiz_true)) {
coordinator.notifyCorrectQuizNum();
Log.d(QUIZ_DIALOG,"\t\tCorrect answer! it was true");
}
coordinator.setQuizEnded(true);
if(questionNumber > 0)
loadNewQuestionIntoDialog(dialog, coordinator.updateQuizLeft());
}
});
dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// User clicked to False btn
if (answer.equals(quiz_false)){
Log.d(QUIZ_DIALOG,"\t\tCorrect Answer!! It was false");
coordinator.notifyCorrectQuizNum();
}
coordinator.setQuizEnded(true);
if(questionNumber > 0)
loadNewQuestionIntoDialog(dialog, coordinator.updateQuizLeft());
}
});
}