UserK UserK - 17 days ago 7
Android Question

Update content of a Dialog with custom view after Click

I am using a Dialog to show a sequence of quizzes in Android. I would like to show the next quiz once the user answers True or False.

I am using a custom layout and I am controlling it with the following code:

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();
}


Let's say there are 100 quizzes to show and a method of the Coordinator class that returns the number of remaining quizzes.

So, after the call

coordinator.setQuizEnded(true);
coordinator.updateQuizLeft();


the
coordinator.getQuizLeft()
will return 99. And
coordinator.getQuiz()
will point to the next quiz.

How do I update the
TextView question
and
numQ
without closing the dialog?

Answer

Try something like this :

setupQuizDialog() which is called first and creates a dialog without any content.

loadNewQuestionIntoDialog() which is continuously called to refresh the content of the dialog with a new question.

    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());
        }
    });

}
Comments