I want to set my setOnShowListener() method to prevent dialog from closing, so I write something like this,
public Dialog update_pop_up()
{
final AlertDialog.Builder builder = new AlertDialog.Builder(UgJadwal.this);
LayoutInflater inflater = UgJadwal.this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.updatepopup, null);
builder.setView(dialogView);
builder.setIcon(R.drawable.update);
builder.setTitle("Update schedule");
builder.setPositiveButton(R.string.update, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int id)
{
}
});
builder.setNeutralButton(R.string.retrieve, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int id)
{
}
});
builder.setNegativeButton(R.string.cancel, null);
return builder.create();
builder.setOnShowListener(new DialogInterface.OnShowListener()
{
@Override
public void onShow(DialogInterface dialog)
{
Button retrieve = builder.getButton(AlertDialog.BUTTON_NEUTRAL);
retrieve.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//do nothing;
}
});
}
});
}
At least there are two error prompted by eclipse, the first one is on the setOnShowListener
that say "The method setOnShowListener(new DialogInterface.OnShowListener(){}) is undefined for the type AlertDialog.Builder" and the other one is on the builder.getButton(AlertDialog.BUTTON_NEUTRAL)
that say "The method getButton(int) is undefined for the type AlertDialog.Builder". I just wonder why it raises errors becuase I have do it right I think. Does everyone have an idea to fix it, thanks.
[UPDATE!!]
Thanks everybody for your help. This is my final methoD on setOnShowListener()
dialog.setOnShowListener(new DialogInterface.OnShowListener()
{
@Override
public void onShow(DialogInterface dialog)
{
Button retrieve = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEUTRAL);
retrieve.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
datasource.opentoread();
String[] hari = datasource.fetchDaily(ids.getText().toString());
datasource.close();
day.setText(String.valueOf(hari[0]));
subject.setText(String.valueOf(hari[1]));
time.setText(String.valueOf(hari[2]));
location.setText(String.valueOf(hari[3]));
lecture.setText(String.valueOf(hari[4]));
Toast.makeText(getApplicationContext(), "Retrieve success", Toast.LENGTH_LONG).show();
}
});
}
});
return dialog;
There are two reasons why you'll get an error with this:
return
.setOnShowListener()
is a method in AlertDialog, not AlertDialog.Builder.This is the same problem,
getButton()
is a method in AlertDialog, not AlertDialog.Builder.Try something like this:
Or instead of creating an AlertDialog, you could just use a standard Dialog and then dismiss it when you want or listen to when the user closes the dialog. This would be a better alternative instead of using the default behavior of a button click closing the Dialog (AlertDialog).
Looking at the AlertDialog.Builder documentation,
setOnShowListener
is not a valid method ofAlertDialog.Builder
. However, it is a valid method ofAlertDialog
, which is the return type forbuilder.create()
. Similarly forgetButton
which is actually a method ofAlertDialog
. Therefore your code should be: