如何在onCreate方法中将来自onActivityResult的数据添加到tablelayout的特定选定行?

时间:2012-06-04 07:30:22

标签: android tablelayout

final TableRow tr1_vin_details = new TableRow(this);
inspStatus = new ImageView(this);
tr1_vin_details.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
 // TODO Auto-generated method stub
 //TableRow tr = (TableRow) v.getParent();
 TableRow lTableRow = ((TableRow) v);
 TextView lTextView = (TextView) lTableRow.getChildAt(1);
 vinNum = lTextView.getText().toString();
 theTag = (Integer) v.getTag();
 Intent intent = new Intent(DeliveryInspectionActivity.this, ExceptionsActivity.class);
 //intent.putExtra("Row ID of a particular selected row:", rowId);
 intent.putExtra("Tag value", theTag);
 startActivityForResult(intent, 0);

} });

现在我想将行id(theTag)值发送到nextActivity,而在onActivityResult中我想添加一些来自NextActivity的数据。代码是

protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (resultCode == RESULT_OK) {
    id_row = data.getIntExtra("id of row:", 0);
    String str = String.valueOf(id_row);
    Toast.makeText(this, "You have selected row: " + " " + str, Toast.LENGTH_SHORT).show();
    int num_exceptions = data.getIntExtra("Number Of Exceptions", 0);
    TextView area_tv = new TextView(this);
    TextView type_tv = new TextView(this);
    TextView severity_tv = new TextView(this);
    if(num_exceptions > 0){
        inspStatus.setBackgroundResource(R.drawable.ir_vin_red_check);
        tv_area.setText(Html.fromHtml("<br> Area: "));
        tv_area.setTextColor(Color.BLACK);
        tv_type.setText(Html.fromHtml("<br> Type: "));
        tv_type.setTextColor(Color.BLACK);
        tv_severity.setText(Html.fromHtml("<br> Severity: "));
        tv_severity.setTextColor(Color.BLACK);
        openedRow1.removeAllViews();
        openedRow1.addView(tv_area);
        openedRow1.addView(tv_type);
        openedRow1.addView(tv_severity);

        openedRow2.removeAllViews();
        openedRow2.addView(area_tv);
        openedRow2.addView(type_tv);
        openedRow2.addView(severity_tv);

        tl_skiddetails.addView(openedRow1, id_row);
        tl_skiddetails.addView(openedRow2, id_row);
    }
}

}

现在我的问题是我要添加opensRow1&amp;在firstactivity中点击togglebutton时,在FirstActivity的onCreate方法中打开第2行到表格布局

我应该在onActivityResult或onCreate方法中添加这些行...?当我在OnActivityResult中添加它时,应用程序强制关闭。

06-04 12:40:58.216:E / AndroidRuntime(1642):java.lang.RuntimeException:传递结果失败ResultInfo {who = null,request = 0,result = -1,data = Intent {(有额外内容) }} to activity {net.mss.palsapp / net.mss.palsapp.DeliveryInspectionActivity}:java.lang.IllegalStateException:指定的子节点已经有父节点。您必须首先在孩子的父母身上调用removeView()。

请帮助我...我很久就在努力解决这个问题...

1 个答案:

答案 0 :(得分:1)

如果您想动态地将表格行添加到表格布局,请遵循以下代码

// Get the TableLayout
    TableLayout tl = (TableLayout) findViewById(R.id.maintable);

 // Create a TableRow and give it an ID
        TableRow tr = new TableRow(this);
         tr.setId("unique id for table row");
        tr.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));


         // Create a TextView to house the name of the province
        TextView labelTV = new TextView(this);
        labelTV.setId("unique id");
        labelTV.setText("your text");
        labelTV.setTextColor(Color.BLACK);
        labelTV.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        tr.addView(labelTV);


        // Add the TableRow to the TableLayout
        tl.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));