如何为每个循环创建一个switch case - android studio

时间:2016-10-12 15:55:19

标签: java android android-studio

这里我有一个用于对话单选的3种选择的开关盒。 目前我的序列中有3个项目,但这不是我想要的更多我想要添加25个选项而我不认为它的好编码有24个案例(包括0)我怎么能复制我在对于每个循环?

public class Multiselection extends DialogFragment {

final CharSequence[] items={"easy","medium","hard"};
String selection;
@Override
@NonNull
public Dialog onCreateDialog(Bundle savedInstanceState)
{
    final String [] items = getResources().getStringArray(R.array.my_drinks_selection); //gets array items to screen
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Choose drink").setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

            switch (which) {
                case 0:
                    selection = (String) items[which];

                    break;
                case 1:
                    selection = (String) items[which];

                    break;
                case 2:
                selection = (String) items[which];

                break;
            }

        }

1 个答案:

答案 0 :(得分:2)

public class Multiselection extends DialogFragment {

final CharSequence[] items={"easy","medium","hard"};
String selection;
@Override
@NonNull
public Dialog onCreateDialog(Bundle savedInstanceState)
{
final String [] items = getResources().getStringArray(R.array.my_drinks_selection); //gets array items to screen
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Choose drink").setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        selection = items[which];
    }
}