Stelios M
Stelios M

Reputation: 237

Can i close an alert dialog on list click?

I have an Alert Dialog which has a list on it, and i want to close onlistclick is it possible?

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

    final String[] Categories = SQLiteHelper.getAllCategories();//this is where i get the array for my list
    ListView myList = new ListView(this);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.alert_dialog_list_view, Categories);
    myList.setAdapter(adapter);
    myList.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
                     //doing something in here and then close
         }
    });
    builder.setTitle("Please Choose");
    builder.setInverseBackgroundForced(true);
    builder.setView(myList);
    final Dialog dialog = builder.create();
    dialog.show();
    }

The alert dialog is running perfect i just dont want to put any buttons in it.

Upvotes: 7

Views: 4674

Answers (2)

Sandip Jadhav
Sandip Jadhav

Reputation: 7155

check below code

 myList.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {

              //doing something in here and then close
              dialog.dismiss();
         }
    });

Upvotes: 2

Jave
Jave

Reputation: 31846

If you define the onItemClickListener after the Dialog you can just call dialog.dismiss(); in the onItemClick() method.

Upvotes: 13

Related Questions