Search code examples
javaandroidxmltextviewfinal

Assign values to textview without final string


I got a problem in setting textview values.

String ID="";
......(code to assign ID values)

TextView textViewToChange = (TextView) v.findViewById(R.id.textview1);

textViewToChange.setText(ID);

I got an error that let change ID to final. but the ID is changeable, can't be final. I tried EditText instead TextView, but got the same problem.

Is there any suggestions?

Thank you.


Solution

  • I guess you use your variable in some anonymous inner class so it has to be final. But it's changeable so it must not be final. This can be solved by using final array of size one.

    final String[] ID= new String[1];
    ID[0] = //assignment logic
    ...
    textViewToChange.setText(ID[0]);
    

    So your ID is final but you can change the values inside the array.