android - java中的switch case语句中,case所对应的数目是不确定的,能否动态改变case 的个数?
PHP中文网
PHP中文网 2017-04-18 09:37:56
0
2
434

现在方法中传入的menuList的数目是不确定的,每个menuList.get(i),都对应一个runnableList.get(i)。现在需要根据menuList.size()动态改变case的数目,求思路

public boolean Confim(final Activity act, String title, final String[] menuList, final List<Runnable> runList) {
        runnableList = runList;
        AlertDialog.Builder builder = new AlertDialog.Builder(act);
        builder.setTitle(title);
        builder.setItems(menuList, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int which) {

                switch (which) {
                    case 0:
                        if (runnableList.get(0) == null) return;
                        runnableList.get(0).run();
                        break;
                    case 1:
                        if (runnableList.get(1) == null) return;
                        runnableList.get(1).run();
                        break;
                    case 2:
                        if (runnableList.get(2) == null) return;
                        runnableList.get(2).run();
                        break;
                }
            }
        });
    
        builder.show();

        return false;
    }
PHP中文网
PHP中文网

认证高级PHP讲师

全員に返信(2)
黄舟

switch case はコンパイル時に指定され、動的に変更できません。
また、あなたのご要望はこの方法で実現できないでしょうか?

リーリー
いいねを押す +0
刘奇

このコードでは、パラメータとして where を渡すだけです。なぜ switch を使用するのですか?

いいねを押す +0
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!