rala
rala

Reputation: 897

AlertDialog Extern - all fine but not returning the value as I wish

I'm trying to make a extern class for AlertDialog. I want to have an universal class to use it quickly.
I know the code isn't difficult at all, but there are anyhow many rows to write (or copy) and if I would find a mistake I maybe had to change many code...
I've everything but one thing I don't get.
So it works but returning the correct onClick doesn't work.
I've also tried to make an while loop before returning, but then the app is hanging....
Has somebody any idea?

public class RalaAlertDialog{
private static AlertDialog.Builder alertDialog;
private static long onClick=RalaInterfaceDefault.FehlerSpezialZahl;

//neutralButton
public static long AlertDialogNeutral(Context class_this, String mssg, String ntrlBttnTxt, boolean dismissable, String title){
    onClick=RalaInterfaceDefault.FehlerSpezialZahl; //default error number
    alertDialog=new AlertDialog.Builder(class_this);
    if(mssg.equals("")){
        mssg="DEFAULT-TEXT";
    }
    if(title.equals("")){
        title="DEFAULT-TITLE";
    }
    if(ntrlBttnTxt.equalsIgnoreCase("")){
        System.out.println("No values set - default in use.");
        ntrlBttnTxt="OK";
    }
    alertDialog.setMessage(mssg)
    .setCancelable(dismissable);
    alertDialog.setTitle(title);

    alertDialog.setPositiveButton(ntrlBttnTxt,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id){
                    onClick=0;
                    dialog.dismiss();
                }
            }
    );

    AlertDialog a=alertDialog.create();
    a.show();
    //wait until button is click before continuing
    return onClick;
}

Upvotes: 0

Views: 74

Answers (1)

ElDuderino
ElDuderino

Reputation: 3263

public static AlertDialog getAlertDialog(Context ctx, String title, String message, String posButton, boolean dismissable, final DialogInterface.OnClickListener ocl) {

    AlertDialog.Builder builder =new AlertDialog.Builder(ctx);
    builder.setTitle(title)
        .setMessage(message)
        .setCancelable(dismissable)
        .setPositiveButton(posButton,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id){
                    dialog.dismiss();
                    if(ocl!=null) ocl.onClick(dialog, id);
                }
            });

    AlertDialog dialog = builder.create();
    return dialog;
}

Use it like this :

    AlertDialog dialog = getAlertDialog(this,"Hello","World","OK",false,new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Log.i("DIALOG","OK Clicked");
        }
    });
    dialog.show();

Of course you need only one OnClickListener, but I like it better that way.

Upvotes: 1

Related Questions