Reputation: 29
I want to pass a Method (SaveClound) as a parameter (AlertDialog Parameter) so i can use differents methods through this parameter (in actionButtons Method).
public void actionButtons(){
buttonVoltar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
alertDialog(saveClound());
// see? I want to call the a method through this parameter
}
});
}
public void alertDialog(Method methodName) {
AlertDialog.Builder builderaction = new AlertDialog.Builder(this);
builderaction.setTitle("Atenção!");
builderaction.setMessage("Você tem certeza que deseja sair?");
builderaction.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// i want to call here the paramater i'm passing on this method (methodName)
// so i can call any methods i want right here
}
});
builderaction.setNegativeButton("No",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builderaction.create();
alert.setIcon(R.drawable.ic_stop);
alert.show();
}
public void saveClound(){
Toast.makeText(getApplicationContext(), "ABC", Toast.LENGTH_SHORT).show();
}
Upvotes: 1
Views: 2462
Reputation: 4066
You can do it by passing a runnable to the method for example
public void actionButtons(){
buttonVoltar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Runnable runnable = new Runnable() {
@Override
public void run() {
saveClound();
}
};
alertDialog(runnable);
}
});
}
public void alertDialog(Runnable runnable) {
AlertDialog.Builder builderaction = new AlertDialog.Builder(this);
builderaction.setTitle("Atenção!");
builderaction.setMessage("Você tem certeza que deseja sair?");
builderaction.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// i want to call here the paramater i'm passing on this method (methodName)
// so i can call any methods i want right here
new Handler().post(runnable);
}
});
builderaction.setNegativeButton("No",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builderaction.create();
alert.setIcon(R.drawable.ic_stop);
alert.show();
}
public void saveClound(){
Toast.makeText(getActivity(), "ABC", Toast.LENGTH_SHORT).show();
}
Upvotes: 1