从内部类访问变量而不是最终的

时间:2013-08-26 13:21:55

标签: java android variables inner-classes final

我怎么能让这个工作:

public void onStart()
        {
            super.onStart();

            int dayN = 0;
            int i = 0;
            String day = null;
            String addFach;

            String mo1 = null;
            String mo2 = null;
            String mo3 = null;
            String mo4 = null;
            String mo5 = null;
            String mo6 = null;
            String mo7 = null;
            String mo8 = null;
            String mo9 = null;
            String mo10 = null;
            String mo11 = null;

            String di1 = null;
            String di2 = null;
            String di3 = null;
            String di4 = null;
            String di5 = null;
            String di6 = null;
            String di7 = null;
            String di8 = null;
            String di9 = null;
            String di10 = null;
            String di11 = null;

            String mi1 = null;
            String mi2 = null;
            String mi3 = null;
            String mi4 = null;
            String mi5 = null;
            String mi6 = null;
            String mi7 = null;
            String mi8 = null;
            String mi9 = null;
            String mi10 = null;
            String mi11 = null;

            String do1 = null;
            String do2 = null;
            String do3 = null;
            String do4 = null;
            String do5 = null;
            String do6 = null;
            String do7 = null;
            String do8 = null;
            String do9 = null;
            String do10 = null;
            String do11 = null;

            String fr1 = null;
            String fr2 = null;
            String fr3 = null;
            String fr4 = null;
            String fr5 = null;
            String fr6 = null;
            String fr7 = null;
            String fr8 = null;
            String fr9 = null;
            String fr10 = null;
            String fr11 = null;

            while (dayN <= 4)
            {
                if (dayN == 0)
                {
                    day = "Montag";
                } else if (dayN == 1)
                {
                    day = "Dienstag";
                } else if (dayN == 2)
                {
                    day = "Mittwoch";
                } else if (dayN == 3)
                {
                    day = "Donnerstag";
                } else if (dayN == 4)
                {
                    day = "Freitag";
                }

                while (i <= 10)
                {

                    BufferedReader buf;
                    try
                    {
                        buf = new BufferedReader(new FileReader("/sdcard/" + i + day + "fach.txt"));
                        addFach = buf.readLine();

                        if (day == "Montag")
                        {
                            if (i == 0) mo1 = addFach;
                            else if (i == 1) mo2 = addFach;
                            else if (i == 2) mo3 = addFach;
                            else if (i == 3) mo4 = addFach;
                            else if (i == 4) mo5 = addFach;
                            else if (i == 5) mo6 = addFach;
                            else if (i == 6) mo7 = addFach;
                            else if (i == 7) mo8 = addFach;
                            else if (i == 8) mo9 = addFach;
                            else if (i == 9) mo10 = addFach;
                            else if (i == 10) mo11 = addFach;
                        } else if (day == "Dienstag")
                        {
                            if (i == 0) di1 = addFach;
                            else if (i == 1) di2 = addFach;
                            else if (i == 2) di3 = addFach;
                            else if (i == 3) di4 = addFach;
                            else if (i == 4) di5 = addFach;
                            else if (i == 5) di6 = addFach;
                            else if (i == 6) di7 = addFach;
                            else if (i == 7) di8 = addFach;
                            else if (i == 8) di9 = addFach;
                            else if (i == 9) di10 = addFach;
                            else if (i == 10) di11 = addFach;
                        } else if (day == "Mittwoch")
                        {
                            if (i == 0) mi1 = addFach;
                            else if (i == 1) mi2 = addFach;
                            else if (i == 2) mi3 = addFach;
                            else if (i == 3) mi4 = addFach;
                            else if (i == 4) mi5 = addFach;
                            else if (i == 5) mi6 = addFach;
                            else if (i == 6) mi7 = addFach;
                            else if (i == 7) mi8 = addFach;
                            else if (i == 8) mi9 = addFach;
                            else if (i == 9) mi10 = addFach;
                            else if (i == 10) mi11 = addFach;
                        } else if (day == "Donnerstag")
                        {
                            if (i == 0) do1 = addFach;
                            else if (i == 1) do2 = addFach;
                            else if (i == 2) do3 = addFach;
                            else if (i == 3) do4 = addFach;
                            else if (i == 4) do5 = addFach;
                            else if (i == 5) do6 = addFach;
                            else if (i == 6) do7 = addFach;
                            else if (i == 7) do8 = addFach;
                            else if (i == 8) do9 = addFach;
                            else if (i == 9) do10 = addFach;
                            else if (i == 10) do11 = addFach;
                        } else if (day == "Freitag")
                        {
                            if (i == 0) fr1 = addFach;
                            else if (i == 1) fr2 = addFach;
                            else if (i == 2) fr3 = addFach;
                            else if (i == 3) fr4 = addFach;
                            else if (i == 4) fr5 = addFach;
                            else if (i == 5) fr6 = addFach;
                            else if (i == 6) fr7 = addFach;
                            else if (i == 7) fr8 = addFach;
                            else if (i == 8) fr9 = addFach;
                            else if (i == 9) fr10 = addFach;
                            else if (i == 10) fr11 = addFach;
                        }
                    }
                    catch (Exception e)
                    {

                    }

                    i++;
                }

                i = 0;
                dayN++;
            }

            RadioButton rB1 = (RadioButton) getView().findViewById(R.id.stunde1);
            RadioButton rB2 = (RadioButton) getView().findViewById(R.id.stunde2);
            RadioButton rB3 = (RadioButton) getView().findViewById(R.id.stunde3);
            RadioButton rB4 = (RadioButton) getView().findViewById(R.id.stunde4);
            RadioButton rB5 = (RadioButton) getView().findViewById(R.id.stunde5);
            RadioButton rB6 = (RadioButton) getView().findViewById(R.id.stunde6);
            RadioButton rB7 = (RadioButton) getView().findViewById(R.id.stunde7);
            RadioButton rB8 = (RadioButton) getView().findViewById(R.id.stunde8);
            RadioButton rB9 = (RadioButton) getView().findViewById(R.id.stunde9);
            RadioButton rB10 = (RadioButton) getView().findViewById(R.id.stunde10);
            RadioButton rB11 = (RadioButton) getView().findViewById(R.id.stunde11);
            DatePicker dP = (DatePicker) getView().findViewById(R.id.datePicker);

            dP.init(dP.getYear(), dP.getMonth(), dP.getDayOfMonth(), new DatePicker.OnDateChangedListener()
            {
                public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
                {
                    Calendar cal = Calendar.getInstance();
                    cal.set(year, monthOfYear, dayOfMonth);
                    int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

                    switch(dayOfWeek)
                    {
                        case Calendar.MONDAY:
                            rB1.setText(mo1);
                            break;
                    }

                }
            });
        }

它应该根据所选日期的工作日和datepicker设置一些radiobuttons的文本。 我不能将字符串设置为final,因为它们被赋值为2次。

3 个答案:

答案 0 :(得分:4)

不,不可能。

JLS # chapter 8

  

使用但未在内部类中声明的任何局部变量,形式参数或异常参数必须声明为final。

     

在内部类的主体之前,必须明确赋值(§16)使用但未在内部类中声明的任何局部变量。

作为旁注:使用Collections而不是声明许多字段。

以下是如何从docs中访问它们的示例:

class WithDeepNesting {
    boolean toBe;
    WithDeepNesting(boolean b) { toBe = b; }

    class Nested {
        boolean theQuestion;
        class DeeplyNested {
            DeeplyNested(){
                theQuestion = toBe || !toBe;  // here you are able to access now.
            }
        }
    }
}

答案 1 :(得分:3)

如果您将它们声明为成员变量(在方法之外,最好在onCreate()之前,以便于阅读),它们将在Activity甚至Activity的内部类中被访问

答案 2 :(得分:1)

通常的做法是将它们转换为成员变量。但是,在您的情况下,您需要重新考虑您的设计。

你有太多的代码重复。你应该重构它以使用一个集合,此时内部类问题会自行解决。