我个人觉得使用和阅读切换案例场景更容易。有谁知道我的列表视图的代码可以更改为什么,以便它使用字符串上的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 :(得分: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
答案 1 :(得分:1)
创建并使用Factory方法..
Fragment newFragment = FragmentFactory.createInstance(country, view);
...
Intent intent = IntentFactory.createInstance(country, view);
这样,您可以在Factory类中添加新类型的Fragments和Intents,但在客户端类中保留通用类型。它以这种方式更易于维护和扩展
请考虑此帖子,以便对此主题进行另一次讨论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;
}