在switch语句中保存值

时间:2018-01-30 07:23:41

标签: android switch-statement settext getvalue

在listview alertdialog中选择切换案例中的项目之后如何将值保存到textview,谢谢

    final AlertDialog.Builder builder = new 
    AlertDialog.Builder(FlightActivity.this);
    builder.setTitle("Pilih Kota Asal");
    // add a list
    final String[] asalkota = {"Jakarta", "Medan", "Palembang", "Bandung", 
    "Yogyakarta", "Palu", "Makassar", "Padang", "Surabaya", "Palangkaraya", 
    "Solo", "Denpasar", "Madura"};
    builder.setItems(asalkota, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case 0: 
                case 1: 
                case 2: 
                case 3: 
                case 4: 
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                case 10:
                case 11:
                case 12:

                    kotaAsalFlight.setText(asalkota.toString());
            }
        }
    });

    AlertDialog dialog = builder.create();
    dialog.show();
   }

3 个答案:

答案 0 :(得分:0)

在每种情况下都使用此代码:

kotaAsalFlight.setText(asalkota[Your Case Number Here].toString()));
break;

答案 1 :(得分:0)

您不需要开关盒。只需覆盖onClick方法,如下所示:

@Override
public void onClick(DialogInterface dialog, int which) {
   kotaAsalFlight.setText(asalkota[which]);
}

答案 2 :(得分:0)

更改此行:kotaAsalFlight.setText(asalkota.toString());

to: kotaAsalFlight.setText(asalkota [which]);

或 将switch case实现更改为:

@Override
public void onClick(DialogInterface dialog, int which) {
   kotaAsalFlight.setText(asalkota[which]);
}