Search code examples
androidarraylisttextviewsettext

TextViews become uneditable after reactivating activity?


So currently I am making an educational application where users can view how to solve a given problem by pressing a button. While my code works fine initially, when I hit the back button and click my View Solution button, my default layout pops up and I cannot edit the last four TextViews. Here is my code:

public class AdditionTensSolutionActivity extends Activity {
public static ArrayList<TextView> explain = new ArrayList<TextView>(3);
public static Button nextstep;
public static int onesnum1 = TensAdditionExerciseActivity.n1display % 10;
public static int onesnum2 = TensAdditionExerciseActivity.n2display % 10;
public static int onesanswer = onesnum1 + onesnum2;
public static int onesanswermod = onesanswer % 10;
public static int tensnum1 = TensAdditionExerciseActivity.n1display / 10;
public static int tensnum2 = TensAdditionExerciseActivity.n2display / 10;
public static int tensanswer = tensnum1 + tensnum2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.showsolutionlayout);

    TextView numrow1 = (TextView) findViewById(R.id.solproblemrow1);
    TextView numrow2 = (TextView) findViewById(R.id.solproblemrow2);
    TextView solution = (TextView) findViewById(R.id.solutiontextview);
    TextView carryover = (TextView) findViewById(R.id.carryovernumbers);

    TextView exp1 = (TextView) findViewById(R.id.explain1);
    TextView exp2 = (TextView) findViewById(R.id.explain2);
    TextView exp3 = (TextView) findViewById(R.id.explain3);
    TextView exp4 = (TextView) findViewById(R.id.explain4);

    numrow1.setText(TensAdditionExerciseActivity.n1display + "");
    numrow2.setText("   " + TensAdditionExerciseActivity.n2display + " +");

    explain.add(exp1);
    explain.add(exp2);
    explain.add(exp3);
    explain.add(exp4);

    nextstep = (Button) findViewById(R.id.nextstep);

    for (int i = 0; i < 4; i++) {
        explain.get(i).setVisibility(View.GONE);
    }

    solution.setVisibility(View.GONE);
    carryover.setVisibility(View.GONE);
    explain.get(0).setText("test");
    setTextViews();
    nextButtonsetOnClickListener();

}

protected void nextButtonsetOnClickListener() {
    nextstep.setOnClickListener(new View.OnClickListener() {
        int i = 0;

        public void onClick(View v) {
            explain.get(i).setVisibility(View.VISIBLE);
            i++;

            if (i > 2 && onesanswer < 10) {
                nextstep.setClickable(false);

            }

            if (i > 3 && onesanswer >= 10) {
                nextstep.setClickable(false);

            }
        }
    });

}

protected void setTextViews() {

    explain.get(0).setText(
            "Add " + (onesnum1) + " and " + (onesnum2) + " which equals "
                    + (onesanswer) + ".");

    if (onesanswer >= 10) {

        explain.get(1).setText(
                "Since the answer is 10 or greater, 1 must carry over to the tens place and "
                        + onesanswermod + " is left in the ones place.");
        explain.get(2).setText(
                "Add the tens place digits, " + tensnum1 + " and "
                        + tensnum2
                        + ". Don't forget to add the carried over 1!");
        explain.get(3).setText(
                "1 + " + tensnum1 + " + " + tensnum2 + " = "
                        + (tensanswer + 1));

    } else {
        explain.get(1).setText(
                "Add the tens place digits: " + tensnum1 + " and "
                        + tensnum2 + ".");

        explain.get(2).setText(
                tensnum1 + " + " + tensnum2 + " = " + tensanswer);
    }

Ah, I figured it out. I had my ArrayList set to static rather than final, but I still do not completely the entirety of my error. Would someone be willing to tell me why it made such a big difference?


Solution

  • A static variable / method has only one instance for the entire class. That means, that in the case of your listview, only one exists for all instances of your activity in the entire app, which is why your getting your error. Final means that it can't be initialized anywhere other than the constructor or when the variable is defined. (Difference between Static and final?).