I have a listview
which contains elements, and a button delete, when I click on this button I want to display a dialog
then confirm or not the deleting of the element, the problem is that I can't display the dialog on the listview
, I have an issue with the context.
public void onClick(View v) {
if(v.getId()==R.id.Supprimer){
AlertDialog.Builder builder = new
AlertDialog.Builder(getContext());
builder.setCancelable(true);
builder.setTitle("Suppression d'un rendez-vous");
builder.setMessage("Voulez vous supprimer ce rendez-vous");
builder.setNegativeButton("Non", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
builder.setPositiveButton("Oui", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//alertTextView.setVisibility(View.VISIBLE);
GestBDD.suppRDV(rdv,getContext());
adapter.remove(getPosition(rdv));
cl.notifyDataSetChanged();
Toast.makeText(getContext(),"Rendez vous
supprimé",Toast.LENGTH_LONG).show();
}
});
builder.show();
}
}
the error is at builder.show() line and it says
W/System.err: at
com.example.hp.bendaoudtest.RDVAdapter.onClick(RDVAdapter.java:110)
AlertDialog only accepts activity context instead of application context. If your getContext()
gets application context you are going to get an error. Your getContext()
method should return activity context which can be instantiated from activity class you are using by sending this
as constructor parameter. Don't send getApplicationContext()
as constructor parameter. Example:
RVDAdapter rvdadapter = new RVDAdapter(this);