【问题标题】:Tablelayoutlooses inner TableRows after call subintent调用 subintent 后 Tablelayout 释放内部 TableRows
【发布时间】:2011-12-28 05:28:59
【问题描述】:

我有一个包含很多字段的表单。在调用一个新的 Intent(相机)并返回到主要活动后,所有字段都被填充。好的。

但是 tablelayout 失去了内部 TableRows。为什么?有没有办法保留tablerows?

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

【问题讨论】:

    标签: android android-layout tablelayout xoom


    【解决方案1】:

    如果您在 oncreate() 方法中创建 TableLayout 而在 onResume() 方法中不执行任何操作,则不会丢失任何行

    示例代码

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

    【讨论】:

    • TableLayout 和各自的 TableRows 已经在 onCreate 上。并且没有实现onResume方法(@Override)来调用新的Intent:Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, 0);
    • 检查你是否在onActivityResult中做任何事情。我已经尝试了与 startActivityForResult 相同的示例。它工作正常
    • @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (data.getExtras()!=null &amp;&amp; data.getExtras().containsKey("data")) { Bitmap imagem = (Bitmap) data.getExtras().get("data"); //... TableLayout tl = (TableLayout) findViewById(R.id.tlFotos); LayoutInflater inflater = getLayoutInflater(); TableRow row = (TableRow) inflater.inflate(R.layout.foto_tablerow,null); ((CheckBox) row.getChildAt(0)).setTag(id); ((ImageView) row.getChildAt(1)).setImageBitmap(imagem); tl.addView(row); } } 就这样。
    • 上面的代码运行没有任何异常。感谢您的宝贵时间。
    猜你喜欢
    • 1970-01-01
    • 2011-07-18
    • 2014-09-29
    • 1970-01-01
    • 1970-01-01
    • 2011-12-10
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多