user631063
user631063

Reputation:

Can't get AlertDialog to dismiss when an item is selected

Hey guys, I am having trouble getting my alert dialog to dismiss when an item is selected. I used an AlertDialog builder to display it and I believe that this is my problem. I cannot call dismiss because their is not builder.dismiss() method. Is there a simple way to fix this problem? Here is my code. Thanks.

  AlertDialog.Builder builder = new AlertDialog.Builder(this);

 Context mContext = this;

 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
 View layout = inflater.inflate(R.layout.numplayersdialog,
                                (ViewGroup) findViewById(R.id.layout_root));

 final Spinner spinner = (Spinner) layout.findViewById(R.id.spinner);
 ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
         this, R.array.num_players_array, android.R.layout.simple_spinner_item);
 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 spinner.setAdapter(adapter);

 builder.setTitle("Select Number of Players"); 

 spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3){
            int item = spinner.getSelectedItemPosition();
            commandWriter(item); 
        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });


 builder.setView(layout);
 builder.show();

Upvotes: 0

Views: 1785

Answers (1)

Jason Robinson
Jason Robinson

Reputation: 31283

builder.show() returns an AlertDialog object. You can use this to close the dialog.

Upvotes: 2

Related Questions