1

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.

newbie
  • 958
  • 4
  • 13
  • 25

3 Answers3

6

use this code

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();
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
1

Make students variable final.

final ArrayList<String> students = new ArrayList<String>();
Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
0

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

newbie
  • 958
  • 4
  • 13
  • 25