I have the following code that correctly shows a Custom listView, however I want that when pressing an Item I close the dialog.
On the internet I have found that it is with builder.dismiss();
but I get an error that this method does not exist.
Thank you very much.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_segunda_ad);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
btDialog = findViewById(R.id.btndialog);
btnDl= findViewById(R.id.imgbtn);
btnDl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog();
}
});
btDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog();
}
});
}
public void dialog(){
final AlertDialog.Builder builder = new AlertDialog.Builder(SegundaAd.this);
View customView = LayoutInflater.from(SegundaAd.this).inflate(
R.layout.dilog_layout, null, false);
// builder.setTitle("Simple Dialog");
// builder.setMessage("message");
builder.setView(customView);
ListView listView = (ListView) customView.findViewById(R.id.dialoglist);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,
R.layout.listaitems, listContent);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(SegundaAd.this, ""+listContent[i], Toast.LENGTH_SHORT).show();
/// Aqui cerrar el Dialog
}
});
builder.setPositiveButton(" ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(SegundaAd.this, "OK", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(SegundaAd.this, "CANCEL", Toast.LENGTH_SHORT).show();
}
});
builder.setIcon(R.drawable.kazdos);
builder.show();
}
}