I personally find it easier to use and read switch case scenarios. Does anyone know what this code for my list view can be changed to so that it utilises switch statements on strings instead of if statements? I've changed the compiler to 1.7 as necessary.
@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);
}