如何用户单击确定在AlertDialog之后才开始活动 [英] How to start activity only after user clicks ok on AlertDialog

查看:174
本文介绍了如何用户单击确定在AlertDialog之后才开始活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AlertDialog,我只希望接下来的活动开始后,我点击确定。现在的问题是在我的方法,我有局部变量连接到我的意图,我不能把在onCreateDialog方法。

I have an AlertDialog and I only want the next Activity to start after I have click "OK". The problem is in my method, I have local variables attached to my intent and I cannot put in the onCreateDialog method.

//local variable created
ArrayList<String> students = new ArrayList<String>();    
for(int i=0; i<studentList.size(); i++){
    students.add(studentList.get(i).getName());
}

AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                     new ContextThemeWrapper(this, R.style.popup_theme));
alertDialog.setTitle("Select Game");           
alertDialog.setPositiveButton("3x3", new DialogInterface.OnClickListener() {  
public void onClick(DialogInterface dialog, int which) { 

    //I want the new Activity to start only after user clicks ok.
    Intent intent=new Intent(getApplicationContext(),NewActivity.class);
    //Local variable, undefined, cannot reference 
    intent.putStringArrayListExtra("students", students);
    startActivity(intent);
    finish();
    return;  
    }        
}); 
alertDialog.show();

有没有什么办法可以prevent后的ShowDialog的code()运行,直到用户点击OK?我有我需要把里面的意图局部变量。

Is there any way I can prevent the code after showDialog() from running until the user clicks OK? I have local variable that I need to put inside the intent.

推荐答案

不知道这是否是一个正确的做法,但我做了一个'黑客'通过学生全球变量。欢迎更好的答案。

Not sure if this is a right way but I did a 'hack' by making students a global variable. Would welcome better answers.

这篇关于如何用户单击确定在AlertDialog之后才开始活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆