Search code examples
androidandroid-layouttablelayoutxoom

Tablelayoutlooses inner TableRows after call subintent


I have a form with a lot of fields. After call a new Intent (camera) and back to main activity, all fields are filled. OK.

But tablelayout looses inner TableRows. Why? Is there no way to keep tablerows?

tableMaterialObra = (TableLayout)findViewById(R.id.tlMaterialObra);
TableRow tr = new TableRow(this);
CheckBox cb = new CheckBox(this);
tr.addView(cb);
tableMaterialObra.addView(tr);

Solution

  • If you create TableLayout in oncreate() method and donot do anything in onResume() method then none of the row will be lost

    SampleCode

    import java.util.ArrayList;
    
    import android.app.ActivityGroup;
    import android.content.Intent;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.CheckBox;
    import android.widget.LinearLayout;
    import android.widget.TableLayout;
    import android.widget.TableRow;
    import android.widget.TextView;
    
    public class RelativeLayoutTesting extends ActivityGroup implements OnClickListener{
        TextView choosenGroup;
        android.widget.RelativeLayout currentView = null;
        ArrayList<String> logpoint = null;
        TableLayout tableMaterialObra;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate( savedInstanceState );
    
    
            // set the relative layout as our view 
            setContentView(R.layout.my_table);
            tableMaterialObra = (TableLayout)findViewById(R.id.my_table);
            TableRow tr = new TableRow(this);
            tr.setBackgroundColor(Color.WHITE);
            CheckBox cb = new CheckBox(this);
            tr.addView(cb);
            tableMaterialObra.addView(tr);
            LinearLayout myLayout = (LinearLayout) findViewById(R.id.my_layout);
            myLayout.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            startActivity(new Intent(RelativeLayoutTesting.this, com.sunil.MyListView.class));
        }
    
    }