Creating an AlertDialog with static methods?

I completed most of the game I'm trying to make, and throughout the project I created one specific action that also calls SurfaceView and Thread. I put the update () method in each of the three classes, so each of them knows where the other of them changes something every time. Apparently, the only way to do something like this is to use static methods ... This is normal until a collision occurs in my SurfaceView, and I want to say what to do, what to do. I can pass the information, but then I can not find a way to make AlertDialog.

I understand that I cannot call showDialog () from the Static method, but I cannot find a way to force a non-static method to call it, and then call this method from static. I was looking for an answer, and I heard something about creating an object, but I cannot understand what this means ...

If anyone has a good idea to get me involved in this, please let me know :)

+5
source share
2 answers

Here is what I used:

public static void messageDialog(Activity a, String title, String message){
    AlertDialog.Builder dialog = new AlertDialog.Builder(a);
    dialog.setTitle(title);
    dialog.setMessage(message);
    dialog.setNeutralButton("OK", null);
    dialog.create().show();     

}
+9
source

SurfaceView extends the view and therefore has a getContext () method

To create and show your AlertDialog, you can do the following code inside SurfaceView

AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("title");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
    dialog.dismiss();
    }
});
Dialog d = builder.create();
d.show();

Activity.showDialog(int), ( , ).

,

0

All Articles