我有一个AlertDialog,我只想在单击“确定”后启动下一个Activity。问题在于我的方法,我的局部变量附加到我的意图,我不能放入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()
之后的代码运行,直到用户点击OK?我有局部变量,我需要把它放在意图中。
答案 0 :(得分:6)
使用此代码
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
new ContextThemeWrapper(this, R.style.popup_theme));
alertDialog.setTitle("Select Game");
alertDialog.setMessage("[ Choose Puzzle Matrix ]");
alertDialog.setPositiveButton("3x3", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent myIntent = new Intent(((Dialog) dialog).getContext(), Matrix3x3.class);
startActivity(myIntent);
return;
}
});
alertDialog.show();
答案 1 :(得分:1)
制作students
变量final
。
final ArrayList<String> students = new ArrayList<String>();
答案 2 :(得分:0)
不确定这是否是正确的方法,但我通过使students
成为全局变量来实现“黑客”。欢迎更好的答案。