如何在用户在AlertDialog上单击"确定"后启动活动

new*_*bie 1 android android-alertdialog android-activity

我有一个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();
Run Code Online (Sandbox Code Playgroud)

有没有什么办法可以防止代码showDialog()运行后直到用户点击OK?我有局部变量,我需要放在intent中.

Raj*_*ddy 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();
Run Code Online (Sandbox Code Playgroud)