Reputation: 991
I want to create a function that shows a dialog with 2 buttons on screen and return 1 if user pressed OK and 0 if user pressed Cancel.
public class CDlg {
static int ShowConfirm(String caption, String msg, Context context) {
int rez;
AlertDialog.Builder delAllDialog = new AlertDialog.Builder(context);
delAllDialog.setTitle(caption);
TextView dialogTxt_id = new TextView(context);
LayoutParams dialogTxt_idLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
dialogTxt_id.setLayoutParams(dialogTxt_idLayoutParams);
dialogTxt_id.setText(msg);
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(dialogTxt_id);
delAllDialog.setView(layout);
delAllDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
rez = 1;
}
});
delAllDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
rez = 0;
}
});
delAllDialog.show();
return rez;
}
}
I am now shure that I am doing right because I do not know how to pass a result from unner class to outer one. There is a error message
Cannot refer to a non-final variable rez inside an inner class defined in a different method
So as a result I want to use that function something like this:
if (CDlg.ShowConfirm("User confirmation","Delete?",this)==1){
...
}
Upvotes: 0
Views: 1408
Reputation: 5578
If you want to code in the spirit of Android, you should actually use startActivityForResult. Look at the linked answer for details how it should work. (here is the documentation)
Upvotes: 1
Reputation: 12596
You can't do this like that. ShowConfirm
can only show the dialog. When the user clicks either the OK or Cancel button, only then you can execute what you want:
public class CDlg {
void ShowConfirm(String caption, String msg) {
AlertDialog.Builder delAllDialog = new AlertDialog.Builder(this);
delAllDialog.setTitle(caption);
TextView dialogTxt_id = new TextView(this);
LayoutParams dialogTxt_idLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
dialogTxt_id.setLayoutParams(dialogTxt_idLayoutParams);
dialogTxt_id.setText(msg);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(dialogTxt_id);
delAllDialog.setView(layout);
delAllDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
handleButtonClick(1);
}
});
delAllDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
handleButtonClick(2);
}
});
delAllDialog.show();
}
void handleButtonClick(int rez) {
switch(rez) {
case 1: ..... break;
case 2: ..... break;
.....
}
}
}
The if (CDlg.ShowConfirm("User confirmation","Delete?",this)==1)
statement is useless in Android here, since the ShowConfirm will not wait until the user presses a button.
Instead just call ShowConfirm("User confirmation","Delete?");
an implement the appropriate code in the onClick
s.
Upvotes: 1
Reputation: 24895
Make rez
an attribute instead of a local variable. As your method is static
, the attribute should be too. This means moving the definition outside the method.
public class CDlg {
static int rez;
static int ShowConfirm(String caption, String msg, Context context) {
...
In the inner classes, you need to refer to the CDlg class
public void onClick(DialogInterface arg0, int arg1) {
CDlg.rez = 1;
}
As a side note, it is strange that you use an static
method for this. One of the mistakes of the people new to Java/OOP is to abuse static
code, that feels more like C
was. Maybe you want to reconsider your code.
Upvotes: -1
Reputation: 3881
Define a static variable in the class you want, for example I will define in MyAuxiliaryClass.java:
public static USER_DECISION = -1;
Whenever you choose an option, then you do the following:
if (//Desicion == OK) {
MyAuxiliaryClass.USER_DECISION = 1;
} else (//Decision == NOT OK){
MyAuxiliaryClass.USER_DECISION = 2;
}
Since you are changing this static variable, then you can get the value 1 or 2 in another class. Hope it helps. Best regards.
Upvotes: 0