java - 将 if 字符串语句转换为 switch 字符串语句

标签 java android android-listview switch-statement android-listfragment

我个人发现更容易使用和阅读 switch case 场景。有谁知道我的 ListView 的代码可以更改为什么,以便它在字符串上使用 switch 语句而不是 if 语句?我已根据需要将编译器更改为 1.7。

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        View v = getView();

        if (getActivity().findViewById(R.id.detail_container) != null) {
            mTwoPane = true;
        } else {
            mTwoPane = false;
        }

        ListView lv = (ListView)v.findViewById(android.R.id.list);
        lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {



@Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // get the adapter, then get the name from the adapter at that position
        WorldListAdapter adapter = (WorldListAdapter) parent.getAdapter();
        String country = adapter.getItem(position);

                if (mTwoPane) {
            setItemNormal();
            View rowView = view;
            setItemSelected(rowView);

            Fragment newFragment;
            if (country.equals(view.getResources().getString(R.string.africa))) {
                newFragment = new FragmentAfrica();
            } else if (country.equals(view.getResources().getString(R.string.asia))) {
                newFragment = new FragmentAsia();
            } else if (country.equals(view.getResources().getString(R.string.europe))) {
                newFragment = new FragmentEurope();
            } else {
                newFragment = new FragmentAfrica();
            }
            WorldActivity activity = (WorldActivity) view.getContext();
            FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.detail_container, newFragment);
            transaction.commit();
        } else {
            Intent intent;
            if (country.equals(view.getResources().getString(R.string.africa))) {
                intent = new Intent(getActivity(), AfricaActivity.class);
            } else if (country.equals(view.getResources().getString(R.string.asia))) {
                intent = new Intent(getActivity(), AsiaActivity.class);
            } else if (country.equals(view.getResources().getString(R.string.europe))) {
                intent = new Intent(getActivity(), EuropeActivity.class);
            } else {
                intent = new Intent(getActivity(), AfricaActivity.class);
            }
            startActivity(intent);
        }
    }

            public void setItemSelected(View view) {
                View rowView = view;
                view.setBackgroundColor(Color.parseColor("#1C3F96"));

                TextView tv0 = (TextView) rowView.findViewById(R.id.country);
                tv0.setTextColor(Color.parseColor("#FFFFFF"));

                TextView tv1 = (TextView) rowView.findViewById(R.id.country_description);
                tv1.setTextColor(Color.parseColor("#FFFFFF"));
            }

            public void setItemNormal() {
                for (int i = 0; i < getListView().getChildCount(); i++) {
                    View v = getListView().getChildAt(i);
                    v.setBackgroundColor(Color.TRANSPARENT);

                    TextView tv0 = ((TextView) v.findViewById(R.id.country));
                    tv0.setTextColor(Color.WHITE);

                    TextView tv1 = ((TextView) v.findViewById(R.id.country_description));
                    tv1.setTextColor(Color.parseColor("#B5B5B5"));
                }
            }
        });

        super.onActivityCreated(savedInstanceState);
    }

最佳答案

java 8 支持使用 String 进行 switch case

 @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // get the adapter, then get the name from the adapter at that position
    WorldListAdapter adapter = (WorldListAdapter) parent.getAdapter();
    String country = adapter.getItem(position);

    if (mTwoPane) {
        setItemNormal();
        View rowView = view;
        setItemSelected(rowView);

        Fragment newFragment;
        switch (country.toLowerCase()) {
            case "africa":
                newFragment = new FragmentAfrica();
                break;
            case "asia":
                newFragment = new FragmentAsia();
                break;
            case "europe":
                newFragment = new FragmentEurope();
                break;
            default:
                newFragment = new FragmentAfrica();
        }
        WorldActivity activity = (WorldActivity) view.getContext();
        FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.detail_container, newFragment);
        transaction.commit();
    } else {
        Intent intent;
        switch (country.toLowerCase()) {
            case "africa":
                intent = new Intent(getActivity(), AfricaActivity.class);
                break;
            case "asia":
                intent = new Intent(getActivity(), AsiaActivity.class);
                break;
            case "europe":
                intent = new Intent(getActivity(), EuropeActivity.class);
                break;
            default:
                intent = new Intent(getActivity(), AfricaActivity.class);
        }

        startActivity(intent);
    }
}
           you can replace the literals `africa`,`asia`,`europe` with anything you want

关于java - 将 if 字符串语句转换为 switch 字符串语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31363185/

相关文章:

java - 我的配置文件出现 PlayerJoinEvent 错误

java - 从软键盘捕获 "DONE"按键

android - 更新 AppWidget 后未调用 RemoteViewsFactory 上的 onDataSetChanged()

java - 无法使用 Oracle 数据库连接在 Spring 中加载配置类

java - 将 Jsonpath 输出映射到 POJO 列表

android - 如何从改造 2 响应中获取引用字符串?

Android Studio 无法解析方法 addRule()

android - 相机 Android App Listview

android - ListView、BaseAdapter、getViewTypeCount() - 如何强制适配器再次检查 getViewTypeCount()?

android - 滚动回收器 View 时如何防止项目重复