views:

355

answers:

2

i would like to show a number of alertdialogs so the user has to handle off some questions (a little like a wizzard)

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 1 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 answer (answer should not be 0 here)
A: 

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.

smith324
edited my original question for more detail
Berty
@Berty don't expect the answer outside the `onClickListener` of the `AlertDialog`. Instead save it there and launch the next dialog using `showDialog (int id)` or `showDialog (int id, Bundle args)` These are both methods of the `Activity` class so `CallHandleMenu.this.showDialog(args)` is what you are looking for
smith324
A: 

i fixed it this way (pseudocode) cause 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());
                }
            }
        }
    }
Berty