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

}

现在我的问题是,当 onclick 在 firstactivity 中的切换按钮时,我想在 FirstActivity 的 onCreate 方法中将打开的行 1 和打开的行 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 { (has extras) }}到活动 {net.mss.palsapp/net.mss.palsapp.DeliveryInspectionActivity}:java.lang.IllegalStateException:指定的孩子已经有一个父母。您必须首先在孩子的父母上调用 removeView()。

请帮助我...我长期以来一直在努力解决这个问题...

4

1 回答 1

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));
于 2012-06-04T09:15:20.437 回答