replace
builder.setView(sortByList).create().show();
by
final AlertDialog dialog = builder.setView(sortByList).create();
dialog.show();
Then, in the onItemClick
method, add
dialog.dismiss();
Other solution (cleaner imho)
You could also use AlertDialog.Builder.setAdapter
, which takes an adapter to display a ListView
, and takes a listener to notify you. In that listener you receive a dialog that you can dismiss.
builder.setAdapter(modeAdapter, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
// ### Here you can dismiss the dialog
dialog.dismiss();
switch(which) {
case 0:
Log.d("recent", "was hit");
break;
case 1:
Log.d("title", "was hit");
break;
case 2:
Log.d("author", "was hit");
break;
}
}
}).create().show();