Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use switch case with string resources

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 {
              }
like image 217
wbk727 Avatar asked Jul 31 '15 22:07

wbk727


People also ask

Can you use switch case with Strings?

Yes, we can use a switch statement with Strings in Java.

How do you match a String in a switch case?

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 .

Does switch statement support String data type?

String type is available in the switch statement starting with Java 7.

How do you use multiple cases in a switch case?

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.


3 Answers

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.

like image 29
Nick H Avatar answered Oct 14 '22 05:10

Nick H


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;
    // ...
  }
}
like image 122
Robin Krahl Avatar answered Oct 14 '22 06:10

Robin Krahl


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();
    }
}
like image 42
TABZ Avatar answered Oct 14 '22 06:10

TABZ