Last active
December 19, 2015 19:39
-
-
Save daichan4649/6007899 to your computer and use it in GitHub Desktop.
DialogFragment 定番処理
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<string-array name="list_mode"> | |
<item>@string/mode_edit</item> | |
<item>@string/mode_move</item> | |
</string-array> | |
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface SelectModeDialogListener { | |
void onModeTypeSelected(ModeType selectedMode, int id); | |
} | |
public static DialogFragment newInstance(int id) { | |
Bundle args = new Bundle(); | |
args.putInt("id", id); | |
DialogFragment fragment = new SelectModeDialogFragment(); | |
fragment.setArguments(args); | |
return fragment; | |
} | |
@Override | |
public Dialog onCreateDialog(Bundle savedInstanceState) { | |
final int id = getArguments().getInt("id"); | |
final String[] columns = getResources().getStringArray(R.array.list_mode); | |
AlertDialog.Builder builder = new Builder(getActivity()); | |
builder.setTitle(""); | |
builder.setItems(columns, new OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
ModeType modeType = ModeType.fromTextValue(getActivity(), columns[which]); | |
mListener.onModeTypeSelected(modeType, id); | |
} | |
}); | |
builder.setNegativeButton(R.string.dialog_cancel, new OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
} | |
}); | |
builder.setPositiveButton(R.string.dialog_ok, new OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
} | |
}); | |
return builder.create(); | |
} | |
private SelectModeDialogListener mListener; | |
@Override | |
public void onActivityCreated(Bundle savedInstanceState) { | |
super.onActivityCreated(savedInstanceState); | |
mListener = (SelectModeDialogListener) getActivity(); | |
} | |
public enum ModeType { | |
/** 無効値 */ | |
NONE(-1), | |
/** 編集 */ | |
EDIT(R.string.mode_edit), | |
/** 移動 */ | |
MOVE(R.string.mode_move); | |
private int textResourceId; | |
ModeType(int textResourceId) { | |
this.textResourceId = textResourceId; | |
} | |
public int getTextResourceId() { | |
return textResourceId; | |
} | |
public static ModeType fromTextValue(Context context, String text) { | |
for (ModeType modeType : ModeType.values()) { | |
if (modeType == NONE) { | |
continue; | |
} | |
String itemText = context.getResources().getString(modeType.textResourceId); | |
if (TextUtils.equals(itemText, text)) { | |
return modeType; | |
} | |
} | |
return NONE; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<string name="mode_edit">編集</string> | |
<string name="mode_move">移動</string> | |
<string name="dialog_ok">OK</string> | |
<string name="dialog_cancel">キャンセル</string> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment