【问题标题】:Add, save and delete tablerow添加、保存和删除表格行
【发布时间】:2014-05-28 10:54:03
【问题描述】:

我想创建一个带有buttontable,每次单击它时都会添加一个带有三个TextViewtablerow。目前我只能在没有TextView的情况下添加tablerow,但是,当我关闭应用程序时,创建的行没有保存,谁可以帮助我?

这是我的代码:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btnAddItem = (Button) findViewById(R.id.addItemTableRow);
        btnAddItem.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                TableLayout table = (TableLayout) findViewById(R.id.tableLayout1);

                TableRow row = new TableRow(MainActivity.this);

                TextView t = new TextView(MainActivity.this);

                t.setText("Add table row");

                row.addView(t);

                table.addView(row, new TableLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT));

            }
        });
    }
}

【问题讨论】:

  • 我添加了 tablerow,但是当我关闭应用程序时 tablerow 没有保存。
  • 如果你想保存数据。你试试 SqlLite

标签: android save row android-tablelayout tablerow


【解决方案1】:

您不能将永久视图添加到布局中(“永久”意味着在应用程序关闭后仍然存在)。

如果您指的是应用程序被置于后台(即当用户按下主页按钮时),或者当用户转动设备时,或者当您开始另一个活动并返回时对于这个 android,您可以使用两个回调“保存”您的活动状态: 即onSaveInstanceStateonRestoreInstanceState

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putInt("numRows", rowsNum);
    for (int i=1;i<rowsNum+1;i++) {
         savedInstanceState.putString("row"+i+"1", "Text of the first row first cell");
         savedInstanceState.putString("row"+i+"2", "Text of the first row second cell");
         savedInstanceState.putString("row"+i+"3", "Text of the first row third cell");
    }
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    int numRows = savedInstanceState.getInt("numRows");
    for (int i=1;i<rowsNum+1;i++) {
         String cell1 = savedInstanceState.getString("row"+i+"1");
         String cell2 = savedInstanceState.getString("row"+i+"2");
         String cell3 = savedInstanceState.getString("row"+i+"3");
         //re-create the table here from the strings
    }
}

希望对你有帮助

【讨论】:

  • 感谢您的帮助...我想通过创建 XML 文件来做,但不知道该怎么做
猜你喜欢
  • 2013-02-12
  • 1970-01-01
  • 2012-01-14
  • 1970-01-01
  • 2018-08-23
  • 1970-01-01
  • 1970-01-01
  • 2013-04-18
  • 2018-10-11
相关资源
最近更新 更多