方向更改时保存并恢复TableLayout

时间:2016-12-16 09:36:13

标签: android android-tablelayout onrestoreinstancestate onsaveinstancestate

在我的程序中,我有一个带有一个TableRow的TableLayout,其中我在Button Click上动态添加了更多带有4个TextView的TableRows。现在,如果我改变手机的方向,我想保存并恢复此表。

我有以下代码应该这样做:

 @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);
}

在我的onSavedInstanceState方法中,我通过我的表格,我在StringArray中保存每个Textview。在onRestoreInstanceState中,我尝试重新创建表。

现在我有两个问题。我的第一个问题是,我的程序没有通过onSavedInstanceState中的表格。它只是将最后一行的TextViews添加到我的字符串数组而不是所有行的TextViews。

例如,我有两行:

第1行:&#39; abc&#39; &#39; 123&#39; &#39; DEF&#39; &#39; 456&#39;

第2行:&#39; cba&#39; &#39; 321&#39; &#39;馈&#39; &#39; 654&#39;

现在ABC和CBA应该在我的第一个字符串数组中,但有两次CBA,因为它是最后一行。

我的第二个问题是onRestoreInstanceState。一到达&#39;查看rowchild1 = tableRow.getChildAt(0);&#39;我的程序崩溃了。

有人知道解决方案吗?

1 个答案:

答案 0 :(得分:0)

您应该在使用tableRow

之前初始化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);
        ...
    }
}

同样可能是onRestoreInstanceState中的问题。

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