Reputation: 571
I try to create a class (classCommon) with re-usable methods that I plan to use for different projects.
Now the problem I have is that I want to create a re-usable method in that class for a YES\NO AlertDialog.
So basically I need to code
-an common\reusable AlertDialog that returns "true" (if YES was clicked) or "false"" if NO was clicked
-the main app has to wait til user actually has made a selection
I also have the feeling from my tests that the "main code" won't wait til the user has made a selection - somehow the AlertBox seems runs asyncroniosly (?) - so the main code will be executed regardless what the user selects in the AlertDialog?
So my question is: I want to be able to write code like this:
main code:
//in my main code / Activity:
//call AlertDialog (MessageboxYESNO ) and *wait* til user made a selection, then contine
if ( classCommon._MessageboxYESNO ("Do you want to repeat the game ?","",myActivity.this) == true )
{ //do something if user select YES }
else
{ //do something if user select NO}
...
In classCommon I have created a method that shows the YES\NO AlertDialog - but I don't' know how to return true/false to the calling (main) code.
classCommon: (what I have so far)
public static void (boolean?) _MessageboxYESNO(String sTitle, String sMessage, final Context myContext)
{
AlertDialog.Builder builder = new AlertDialog.Builder(myContext);
builder
.setTitle(sTitle)
.setMessage(sMessage)
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void (boolean?) onClick(DialogInterface dialog, int which) {
//Yes button clicked, do something
//return true ?
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//no button clicked, do something
//return false ?
}
})
.show();
//return true/false ?
}
Upvotes: 1
Views: 1610
Reputation: 543
I think that this is not possible, because it depends on Events.
But I will give you an alternative: Create an Interface with 2 methods: OnYesClicked() and OnNoClicked(). Then you should implement this interface into the class which you invoke the MessageBox method, and pass "this" as a parameter.
The custom Interface:
public interface MyInterface{
public void onYesClicked();
public void onNoClicked();
}
with this, you can do something when you click Yes or No.
public static void _MessageboxYESNO(String sTitle, String sMessage, final Context myContext, MyInterface myInterface)
{
AlertDialog.Builder builder = new AlertDialog.Builder(myContext);
builder
.setTitle(sTitle)
.setMessage(sMessage)
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Yes button clicked, do something
myInterface.onYesClicked();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//no button clicked, do something
myInterface.onNoClicked();
}
})
.show();
}
and this would be your call:
public class AClass implements MyInterface{
@Override
public void onYesClicked(){
//Do Something
}
@Override
public void onNoClicked(){
//Do other thing
}
public void openDialog()
{
classCommon._MessageboxYESNO ("Do you want to repeat the game ?","",myActivity.this,this);
}
}
Upvotes: 3