How can I use the switch case scenario with string resources rather than hard coded names? My strings are exactly the same names used for the spellings of them.
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MainListAdapter adapter = (MainListAdapter) parent.getAdapter();
Main continent = adapter.getItem(position);
if (mTwoPane) {
View rowView = view;
setItemSelected(continent, rowView);
Fragment newFragment;
switch (stringRes) {
case R.id.africa:
newFragment = new FragmentAfrica();
break;
case R.id.asia:
newFragment = new FragmentAsia();
break;
case R.id.europe:
newFragment = new FragmentEurope();
break;
default:
newFragment = new FragmentAfrica();
}
MainActivity activity = (MainActivity) view.getContext();
FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.detail_container, newFragment);
transaction.commit();
} else {
}
Yes, we can use a switch statement with Strings in Java.
To ensure that we have a match in a case clause, we will test the original str value (that is provided to the switch statement) against the input property of a successful match . input is a static property of regular expressions that contains the original input string. When match fails it returns null .
String type is available in the switch statement starting with Java 7.
A switch statement includes literal value or is expression based. A switch statement includes multiple cases that include code blocks to execute. A break keyword is used to stop the execution of case block. A switch case can be combined to execute same code block for multiple cases.
Looks like this could work.. use integers.
public void switchFragments(int stringRes){
if (mTwoPane) {
View rowView = view;
setItemSelected(continent, rowView);
Fragment newFragment;
switch (stringRes) {
case R.id.africa:
newFragment = new FragmentAfrica();
break;
case R.id.asia:
newFragment = new FragmentAsia();
break;
case R.id.europe:
newFragment = new FragmentEurope();
break;
default:
newFragment = new FragmentAfrica();
}
MainActivity activity = (MainActivity) view.getContext();
FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.detail_container, newFragment);
transaction.commit();
}
}
Now all you have to do is call it like this
switchFragments(R.id.africa);
This will go to the case labeled R.id.africa and switch the fragment. It will also work with different languages, because its just an ID of a resource.
Switch labels (that’s the "africa"
in case "africa":
) must be constant expressions according to JLS § 14.12. So if you want to use switch
, you’ll have to store the string resource value in a constant variable and then use that constant as the switch label.
Example (use either class constants or local constants):
class Example {
public static final String CONTINENT_EUROPE =
getResources().getString(R.string.europe); // Option 1: class constants
public static final String CONTINENT_NORTHERN_AMERICA =
getResources().getString(R.string.northernAmerica);
public void foo() {
final String continentAfrica =
getResources().getString(R.string.africa); // Option 2: local constants
final String continentAsia =
getResources().getString(R.string.asia);
switch (continent) {
// Option 1:
case continentAfrica:
newFragment = new FragmentAfrica();
break;
case continentAsia:
newFragment = new FragmentAsia();
break;
// Option 2:
case CONTINENT_EUROPE:
newFragment = new FragmentEurope();
break;
case CONTINENT_NORTHERN_AMERICA:
newFragment = new FragmentNorthernAmerica();
break;
// ...
}
}
I think in this scenario the simplest way is you can just use "simple if else statement" to avoid declaring extra static variables. And also to add support on locale changes.
private String[] titles;
void initialize(){
titles = new String[]{getString(R.string.europe),getString(R.string.africa)};
}
@Override
public Fragment getItem(int position) {
String title = titles[position];
if (title.equals(getString(R.string.europe))) {
return new EuropeFragment();
} else if (title.equals(getString(R.string.africa))) {
return new AfricaFragment();
} else {
return new DefaultFragment();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With