Search code examples
androidandroid-tablelayoutonrestoreinstancestateonsaveinstancestate

Save and restore TableLayout on orientation change


In my program I, have a TableLayout with one TableRow where I added more TableRows with 4 TextViews in it dynamically on a Button Click. Now I want to save and restore this table if I change the orientation of my phone.

I have following code which should do this:

 @Override
protected void onSaveInstanceState(Bundle outState) {
    rowcount = tableLayout.getChildCount();
    for (int i = 0; i < rowcount; i++) {
        View row = tableLayout.getChildAt(i);
        if (row instanceof TableRow) {
            View rowchild1 = tableRow.getChildAt(0);
            if (rowchild1 instanceof TextView) {
                savewarnum[i] = ((TextView) rowchild1).getText().toString();
            }
            View rowchild2 = tableRow.getChildAt(1);
            if (rowchild2 instanceof TextView) {
                savewarname[i] = ((TextView) rowchild2).getText().toString();
            }
            View rowchild3 = tableRow.getChildAt(2);
            if (rowchild3 instanceof TextView) {
                savewarmeng[i] = ((TextView) rowchild3).getText().toString();
            }
            View rowchild4 = tableRow.getChildAt(3);
            if (rowchild4 instanceof TextView) {
                savewarpreis[i] = ((TextView) rowchild4).getText().toString();
            }
        }
    }
    outState.putStringArray("Nummer", savewarnum);
    outState.putStringArray("Name", savewarname);
    outState.putStringArray("Menge", savewarmeng);
    outState.putStringArray("Preis", savewarpreis);
    outState.putInt("Rows", rowcount);
    super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    savewarnum = savedInstanceState.getStringArray("Nummer");
    savewarname = savedInstanceState.getStringArray("Name");
    savewarmeng = savedInstanceState.getStringArray("Menge");
    savewarpreis = savedInstanceState.getStringArray("Preis");
    rowcount = savedInstanceState.getInt("Rows");
    tableLayout = (TableLayout) findViewById(R.id.kaufwaren);
    for(int i=0; i<=rowcount; i++) {
        View tablechild = tableLayout.getChildAt(i);
        if (tablechild instanceof TableRow) {
            View rowchild1 = tableRow.getChildAt(0);
            if (rowchild1 instanceof TextView) {
                ((TextView) rowchild1).setText(savewarnum[i]);
            }
            View rowchild2 = tableRow.getChildAt(1);
            if (rowchild2 instanceof TextView) {
                ((TextView) rowchild2).setText(savewarname[i]);
            }
            View rowchild3 = tableRow.getChildAt(2);
            if (rowchild3 instanceof TextView) {
                ((TextView) rowchild3).setText(savewarmeng[i]);
            }
            View rowchild4 = tableRow.getChildAt(3);
            if (rowchild4 instanceof TextView) {
                ((TextView) rowchild4).setText(savewarpreis[i]);
            }
        }
        createtablerow();
    }
    super.onRestoreInstanceState(savedInstanceState);
}

In my onSavedInstanceState method, I'm going through my table where I save every Textview in a StringArray. In onRestoreInstanceState I'm trying to recreate the table.

Now I have two Problems. My First Problem is, that my Program doesn't go through the table in onSavedInstanceState. It just adds the TextViews of the last row to my string array instead of the TextViews from all Rows.

For example i have two rows:

Row 1: 'abc' '123' 'def' '456'

Row 2: 'cba' '321' 'fed' '654'

Now ABC and CBA should be in my first string array, but there is two times CBA because it's the last row.

My second Problem is in onRestoreInstanceState. As soon as I reach 'View rowchild1 = tableRow.getChildAt(0);' my program crashes.

Does anyone know a solution?


Solution

  • You should initialize tableRow variable before usage in onSaveInstanceState

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        ...
        View row = tableLayout.getChildAt(i);
        if (row instanceof TableRow) {
            TableRow tableRow = (TableRow) row;
            View rowchild1 = tableRow.getChildAt(0);
            ...
        }
    }
    

    Same might be an issue in onRestoreInstanceState.

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        ...
        if (tablechild instanceof TableRow) {
            TableRow tableRow = (TableRow) tablechild;
            View rowchild1 = tableRow.getChildAt(0);
            ...
        }
    }