电话位置更改后,TableLayout变空

时间:2014-12-03 15:06:53

标签: android logcat tablelayout

我的TableLayout使用代码时出现问题 - 动态添加行。

每当我将手机的位置从水平位置更改为垂直位置或从垂直位置更改为水平位置时,输入到我桌面的所有数据都将被删除,并且表格为空。唯一剩下的就是列标题(TextViews),其名称在我的程序中编码。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final TableLayout tableOne = (TableLayout) findViewById(R.id.dataTable1);
    tableOne.setId(110);
    tableOne.setBackgroundColor(Color.WHITE);

    TableRow namesRow = new TableRow(MainActivity.this);
    namesRow.setId(111);
    namesRow.setBackgroundColor(Color.GRAY);
    namesRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));


    TextView productName = new TextView(MainActivity.this);
    productName.setId(120);
    productName.setPadding(2, 0, 40, 5);
    productName.setTextSize(14);
    productName.setTextColor(Color.BLACK);
    productName.setText("Product Name");
    namesRow.addView(productName);

    TextView concentration = new TextView(MainActivity.this);
    concentration.setId(130);
    concentration.setPadding(0, 0, 40, 5);
    concentration.setTextColor(Color.BLACK);
    concentration.setTextSize(14);
    concentration.setText("Concentration");
    namesRow.addView(concentration);

    TextView packages = new TextView(MainActivity.this);
    packages.setId(140);
    packages.setPadding(0, 0, 40, 5);
    packages.setTextSize(14);
    packages.setTextColor(Color.BLACK);
    packages.setText("Packages");
    namesRow.addView(packages);

    TextView volume = new TextView(MainActivity.this);
    volume.setId(150);
    volume.setPadding(0, 0, 40, 5);
    volume.setTextSize(14);
    volume.setTextColor(Color.BLACK);
    volume.setText("Volume");
    namesRow.addView(volume);

    TextView totalamount = new TextView(MainActivity.this);
    totalamount.setId(160);
    totalamount.setPadding(0, 0, 40, 5);
    totalamount.setTextSize(14);
    totalamount.setTextColor(Color.BLACK);
    totalamount.setText("Total Mass");
    namesRow.addView(totalamount);

    TextView sacksnumber = new TextView(MainActivity.this);
    sacksnumber.setId(170);
    sacksnumber.setPadding(0, 0, 40, 5);
    sacksnumber.setText("Number of Sacks");
    sacksnumber.setTextColor(Color.BLACK);
    sacksnumber.setTextSize(14);
    namesRow.addView(sacksnumber);

    tableOne.addView(namesRow, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

上面的代码很好,永远不会消失,但是当我改变代码的位置后,当我从EditText的数据创建每一行时代码正在消失。有点像重置...

Button sncbtn1 = (Button) findViewById(R.id.sncbtn);

    sncbtn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            EditText dataName = (EditText) findViewById(R.id.productField);
            EditText dataConc = (EditText) findViewById(R.id.concField);
            EditText dataPack = (EditText) findViewById(R.id.packageField);
            EditText dataVol = (EditText) findViewById(R.id.volField);

            String prodName = dataName.getText().toString();
            String concValue = dataConc.getText().toString();
            String packValue = dataPack.getText().toString();
            String volValue = dataVol.getText().toString();

            double concDoub = Double.parseDouble(concValue);
            double packDoub = Double.parseDouble(packValue);
            double volDoub = Double.parseDouble(volValue);

            double massResult = volDoub * concDoub;
            double sacksResult = massResult / packDoub;

            System.out.println(prodName + " | " + concValue + " | " + packValue + " | " + volValue);

            TableRow dataRow = new TableRow(MainActivity.this);

            dataRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

            if(tableOne.getChildCount() % 2 == 0) dataRow.setBackgroundColor(Color.LTGRAY);

            TextView prodTable = new TextView(MainActivity.this);
            prodTable.setText(prodName);
            prodTable.setTextSize(14);
            prodTable.setTextColor(Color.BLACK);
            prodTable.setGravity(Gravity.CENTER);
            dataRow.addView(prodTable);

            TextView concTable = new TextView(MainActivity.this);
            concTable.setText(concValue);
            concTable.setTextColor(Color.BLACK);
            concTable.setTextSize(14);
            concTable.setGravity(Gravity.CENTER);
            dataRow.addView(concTable);

            TextView packTable = new TextView(MainActivity.this);
            packTable.setText(packValue);
            packTable.setTextSize(14);
            packTable.setTextColor(Color.BLACK);
            packTable.setGravity(Gravity.CENTER);
            dataRow.addView(packTable);

            TextView volTable = new TextView(MainActivity.this);
            volTable.setText(volValue);
            volTable.setTextSize(14);
            volTable.setTextColor(Color.BLACK);
            volTable.setGravity(Gravity.CENTER);
            dataRow.addView(volTable);

            TextView massTable = new TextView(MainActivity.this);
            massTable.setText(new DecimalFormat("0.00").format(massResult));
            massTable.setTextSize(14);
            massTable.setTextColor(Color.BLACK);
            massTable.setGravity(Gravity.CENTER);
            dataRow.addView(massTable);

            TextView sacksTable = new TextView(MainActivity.this);
            sacksTable.setText(new DecimalFormat("0.00").format(sacksResult));
            sacksTable.setTextColor(Color.BLACK);
            sacksTable.setGravity(Gravity.CENTER);
            sacksTable.setTextSize(14);
            dataRow.addView(sacksTable);

            tableOne.addView(dataRow, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        }
    });

同样在我的LogCat上我收到错误:getExtractedText在非活动的InputConnection上

我试图用一些教程/研究等重写应用程序,认为我可能会发现什么是错的,但是在我完成了2天后,希望有人可以帮助我。

1 个答案:

答案 0 :(得分:0)

每当您的屏幕方向发生变化时,您的onCreate都会被调用,一种方法就是在清单文件中的相关活动标记下添加此行:

android:configChanges="orientation|screenSize"

这样,只要屏幕旋转,Android操作系统就不会重新创建用户界面。