画面に2つのボタンがあるダイアログを表示し、ユーザーが[OK]を押した場合は1を返し、ユーザーが[キャンセル]を押した場合は0を返す関数を作成したいと思います。
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;
}
}
unnerクラスからouterクラスに結果を渡す方法がわからないので、私は今、自分が正しくやっていると確信しています。エラーメッセージがあります
Cannot refer to a non-final variable rez inside an inner class defined in a different method
その結果、私はその関数を次のように使用したいと思います。
if (CDlg.ShowConfirm("User confirmation","Delete?",this)==1){
...
}