0

我个人发现它更易于使用和阅读切换案例场景。有谁知道我的列表视图的代码可以更改为什么,以便它在字符串上使用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); 
    } 
+0

我认为这个问题属于代码审查 –

+1

如果'switch'语句不是最好的解决方案? –

+0

我会建议在多个级别分支,因为在你的例子中处理每个独立的if/else部分集合的方法(不是将它们全部组合到复杂的switch语句中)...更好地处理...另外,可以和更喜欢工厂模式 – Constantin

回答

1

创建和使用工厂方法,而不是..

Fragment newFragment = FragmentFactory.createInstance(country, view); 
...    
Intent intent = IntentFactory.createInstance(country, view); 

这样,您就可以在你的工厂类添加新类型的片段和意图,但留在客户端类通用的类型。这是更容易维护和扩展这样

考虑这个职位另一个就这一话题Simple Factory vs Factory Method: Switch statement in factory vs. client

这里是你的情况可能厂例子

public class FragmentFactory { 

    public static Fragment getInstance(String country, View view) { 
     Fragment newFragment = null; 

     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(); 
      } 
     } 
     return newFragment;  
    } 
} 

,您可以尝试作为开关

public static Fragment getInstance(String country, View view,) { 
    Fragment newFragment = null; 

    switch (country.toLowerCase()) { 

     case view.getResources().getString("africa"): 
      newFragment = new FragmentAfrica(); 
      break; 

     case view.getResources().getString("asia"): 
      newFragment = new FragmentAsia(); 
      break; 

     case view.getResources().getString("europe"): 
      newFragment = new FragmentEurope(); 
      break; 
    }    
    return newFragment; 
} 
+0

你可以编辑回答也显示字符串资源的开关案例senarios吗? – MacaronLover

+0

为什么?其他人已经在另一个答案 – Constantin

+0

已经这样做因为我想看看你会怎么做。 – MacaronLover

2

的java 8支持切换的情况下与String

@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 
+0

很酷。尽管为什么有'default:'那里? – MacaronLover

+0

如果没有'String'匹配你会用'FragmentAfrica'初始化'newFragment',那么你的'if-else-if'阶梯怎么办?我认为它是一个默认情况,这就是为什么我添加它。 – Kainix

+0

如何使用字符串代替硬编码? – MacaronLover