Search code examples
javaandroidswitch-statementandroid-resourcestyped-arrays

Java switch statement completely skipped


I am trying a switch statement to dynamically determine which part of an actionbar dropdown spinner is being selected. This switch statement does not even go to my default case, what am I doing wrong?

public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    // TODO Auto-generated method stub

    //restart fragment with selected spinner item's api call
    TypedArray mArray = getResources().obtainTypedArray(R.array.spinner_actionbar);
    switch(mArray.getResourceId(itemPosition, 0)){
    case R.string.spinner_timeline:
        break;
    case R.string.spinner_profile:
        break;
    case R.string.spinner_top_posts:
        break;
    case 0:
        break;
    default:
        break;
    }
    mArray.recycle();
    return false;
}

mArray.getResourceId(itemPosition, 0) returns the position of an arraylist OR 0, and none of my cases not even case 0 is being called

Thanks for any insight, this is using the android framework


Solution

  • You have empty cases in your switch-case block. You want to do something inside each case, besides just break out of the block. For example:

    case R.string.spinner_timeline:
        System.out.println(R.string.spinner_timeline);
        break;
    case R.string.spinner_profile:
        System.out.println(R.string.spinner_profile);
        break;
    case R.string.spinner_top_posts:
        System.out.println(R.string.spinner_top_posts);
        break;
    case 0:
        break;
    default:
        System.out.println("default case...");
        break;