switch语句未返回正确值

时间:2020-03-02 17:54:38

标签: java android switch-statement adapter

我在回收者视图适配器的getItemViewType中使用switch语句。 getItemViewType中的Switch语句中的所有情况都不会被调用。我打印日志,它说代码没有掉任何switch语句。我没有弄清楚此代码有错误的地方。任何帮助将不胜感激。谢谢

  @Override
    public int getItemViewType(int position) {

        Log.d(TAG, "getItemViewType: Type "+wrapper.getMESSAGE_TYPE()); // it return class 12
        if (Session.getUserID().equals(wrapper.getOTHER_USER_ID())){

            switch (wrapper.getMESSAGE_TYPE()){
                case "class 0":                    
                    return 0;

                case "class 1":
                    return 1;

                case "class 2":
                    return 2;

                case "class 3":
                    return 3;

                case "class 4":
                    return 4;

                case "class 5":
                    return 5;

                case "class 6":
                    return 6;

                case "class 7":
                    return 7;

                case "class 8":
                    return 8;

                case "class 9":
                    return 9;

                case "class 10":
                    return 10;

                case "class 11":
                    return 11;

                case "class 12":
                    Log.d(TAG, "getItemViewType: 12  "+wrapper.getMESSAGE_TYPE()); // it's not calling 
                    return 12;
            }
        }else {

            switch (wrapper.getMESSAGE_TYPE()){
                case "class 0":
                   return 0;
                case "class 1":
                    return 1

                case "class 2":
                    return 2

                case "class 3":
                    return 3

                case "class 4":
                    return 4

                case "class 5":
                    return 5

                case "class 6":
                    return 6

                case "class 7":
                    return 7

                case "class 8":
                    return 8

                case "class 9":
                    return 9

                case "class 10":
                    return 10

                case "class 11":
                    return 11

                case "class 12":             
                Log.d(TAG, "getItemViewType: 12  "+wrapper.getMESSAGE_TYPE()); // it's not calling  either       
                    return 12


            }
        }

         // none of above case called and code then falls into this below code

        Log.d(TAG,"End Of Get Item View Type = "+wrapper.getMESSAGE_TYPE()); // its return class 12
        boolean checkValue;
        if (wrapper.getMESSAGE_TYPE().equals("class 12")){
            Log.d(TAG, "getItemViewType:Check  "+wrapper.getMESSAGE_TYPE());
            checkValue = true;
        }else {
            Log.d(TAG, "getItemViewType:Check 2nd  "+wrapper.getMESSAGE_TYPE());
            checkValue = false;
        }
        Log.d(TAG, "getItemViewType: Check Value ==== "+checkValue); // check value here is true
        return super.getItemViewType(position);
    }

1 个答案:

答案 0 :(得分:0)

是的,String switch-case statements可从Java版本1.7中获得。如果您使用的是以下情况,则没有任何情况会触发。为此,请使用Enum方法,当我们使用该方法时,可以完美地完成这项工作。