Question

I would like to show a number of alertdialogs, so the user has to handle off some questions (a little like a wizard).

Is it possible to make the alertDialog wait until the user chooses something and then returning the choisen value?

    HashMap<Integer, Question> questions = DataConnector.getCallQuestions(position);

    int nextQuestion = position;

    while(nextQuestion != 0){
        Question question = questions.get(nextQuestion);

        CharSequence[] items = (String[])question.getAnswers();

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(question.getQuestion());
        builder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {

            }
        });
        AlertDialog alert = builder.create();

        //I would like to do something like this:
        nextQuestion = alert.getClickedItem();
    }

EDIT. Reponse to chris:

The execution of the program should wait until the user chooses one of the options in the alertDialog

Is this possible?

Like this:

    private int answer = 0;

    ....
    ...methode(){

    //show dialog
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(question.getQuestion());
    builder.setItems(items, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            CallHandleMenu.this.answer = item; //setting answer
        }
    });
    builder.create().show();

    //Here I need to know the answer (the answer should not be 0 here)
Was it helpful?

Solution 2

I fixed it this way (pseudocode), because my code is to dirty :p

    activity{
        onCreate{
            makeNextQuestion(1)
        }

        public void nextQuestion(int questionId){
            //add string and answering buttons to layout
            btn.onClickList(){
                onClick(View btn){
                    activity.this.nextQuestion(btn.getId());
                }
            }
        }
    }

OTHER TIPS

If you want a series of AlertDialog boxes to appear then your best bet would be to set some listeners inside the dialogs themselves.

.setPositiveButton("My Button Name", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           //do stuff like set some variables, maybe display another dialog
                       }
               }) 

Note that I am using the default positive button for the alert dialog, you may need to change this depending on what the user is clicking.

If you are going to be making some call backs into your Activity class from the AlertDialog onClickListener you can use MyActivityClassName.this.myMethodOrVariableHere to do so.

Hope that helps, leave me a comment if you want some more clarification.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top