我有很多不同的狀態,我需要以AlertDialog
的形式向用戶反饋信息。爲每個警報創建一個單獨的類是瘋狂的。我現在擁有的是:如果您不想爲AlertDialogs創建十幾個類,該怎麼辦?
class FeedbackAlertDialog extends DialogFragment {
private String message;
private int action;
FeedbackAlertDialog() {
}
FeedbackAlertDialog(String message, int action) {
this.message = message;
this.action = action;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setCancelable(false)
.setTitle(message)
.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
switch (action) {
case action: // It's impossible because the int should be final
}
startActivity(new Intent(getActivity(), MainActivity.class));
}
}).show();
}
}
的問題是,我不能使用switch
因爲int
應該是最終的。如何處理這種情況?
我認爲你需要**私人詮釋行動; **作爲最終? – 2012-07-17 14:50:47
但是,如果我想'最後'我不能在'構造函數'中改變它。那就是問題所在。 – Eugene 2012-07-17 15:09:02