开发者

AlertDialog gets a return value without a handler

开发者 https://www.devze.com 2023-01-12 16:08 出处:网络
I would like to show a number of alertdialogs, so the user has to handle off some questions (a little like a wizard).

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(it开发者_C百科ems, 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)


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号