0

在 listview alertdialog 的 switch case 中选择一个项目后,如何将值保存到 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();
   }
4

3 回答 3

0

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

kotaAsalFlight.setText(asalkota[Your Case Number Here].toString()));
break;
于 2018-01-30T07:31:36.610 回答
0

你不需要开关盒。只需像这样覆盖 onClick 方法:

@Override
public void onClick(DialogInterface dialog, int which) {
   kotaAsalFlight.setText(asalkota[which]);
}
于 2018-01-30T07:32:57.467 回答
0

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

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

或将 switch case 实现更改为:

@Override
public void onClick(DialogInterface dialog, int which) {
   kotaAsalFlight.setText(asalkota[which]);
}
于 2018-01-30T07:40:25.427 回答