如何仅在用户单击 AlertDialog 上的“确定”后才启动活动
我有一个 AlertDialog,我只想在单击“确定”后启动下一个活动。问题出在我的方法中,我的意图附加了局部变量,并且我无法放入 onCreateDialog 方法。
//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();
有什么方法可以阻止 showDialog()
之后的代码运行,直到用户单击“确定”吗?我有需要将其放入意图中的局部变量。
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();
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用此代码
use this code
将
students
设为final
变量。Make
students
variablefinal
.不确定这是否是正确的方法,但我通过将
students
设为全局变量来进行“黑客攻击”。欢迎更好的答案。Not sure if this is a right way but I did a 'hack' by making
students
a global variable. Would welcome better answers.