How to use the switch case with string resources

How can I use the switch script script with string resources and not hardcoded names? My lines are the same names as for writing their words.

        @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 {
              }

      

+3


source to share


4 answers


Switch labels (i.e. "africa"

in case "africa":

) must be constant expressions in accordance with JLS § 14.12 . So if you want to use switch

, you need to store the string resource value in a constant variable and then use that constant as a radio button 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;
    // ...
  }
}

      

+3


source


It looks like it might 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 jump to the case with the R.id.africa label and switch the snippet. It will also work with different languages ​​because it is just a resource identifier.

+2


source


Just declare them as constants:

public static final String COUNTRY_AFRICA = "africa";
public static final String COUNTRY_ASIA = "asia";

switch (country.toLowerCase()) {
    case COUNTRY_AFRICA:
         newFragment = new FragmentAfrica();
         break;
    // etc..

      

It is better than hardcoding because you can reuse these constants in your application.

If they are UI strings you can declare them in strings.xml

0


source


You must declare it as an enumeration:

public enum Continent{
    africa, asia, europe
}

public Fragment getFragment(String continentKey){
    Continent continent = Continent.valueOf(continentKey);
    return getFragment(continent);
}

public Fragment getFragment(Continent aContinent){
    Fragment newFragment;
    switch(aContinent){
        case asia:
            newFragment = new FragmentAsia();
            break;
        case europe:
            newFragment = new FragmentEurope();
            break;
        case africa: default:
            newFragment = new FragmentAfrica();
            break;
    }
    return newFragment;
}

      

-1


source







All Articles