Android 如何动态删除TableLayout中的行

Android 如何动态删除TableLayout中的行,android,Android,我有一个动态添加行的TableLayout。在每一行中有两个元素,一个是TextView,另一个是Button。当我单击一行中的按钮时,该行应该被删除。在Android中如何做到这一点?如何查找rowid以及如何动态删除行。 有人能帮我解决这个问题吗 提前感谢,尝试这种方法: TableRow tableRow; TextView tv; Button button; //... tableRow.addView(tv) tableRow.addV

我有一个动态添加行的TableLayout。在每一行中有两个元素,一个是TextView,另一个是Button。当我单击一行中的按钮时,该行应该被删除。在Android中如何做到这一点?如何查找rowid以及如何动态删除行。
有人能帮我解决这个问题吗


提前感谢,

尝试这种方法:

    TableRow tableRow;
    TextView tv;
    Button button;
    //...
    tableRow.addView(tv)
    tableRow.addView(button);


    //somewhere where you want to delete
    ViewGroup parent=button.getParent();
    if(parent instanceof TableRow)
        //do deleting task
试试这个:

public void onClick(View v) {
      // TODO Auto-generated method stub

      TableRow t = (TableRow) v.getParent();
      TextView firstTextView = (TextView) t.getChildAt(0);
      code = firstTextView.getText().toString();

      System.out.println("code>>>>>>" + code);

      View row = (View) v.getParent();
      // container contains all the rows, you could keep a
      // variable somewhere else to the container which you
      // can refer to here
      ViewGroup container = ((ViewGroup) row.getParent());
      // delete the row and invalidate your view so it gets
      // redrawn
      container.removeView(row);
      container.invalidate();
}

添加行时,可以指定标记或id。然后,只需使用该标记/id来删除该行

TableLayout table;  // global access, probably initialized in onCreate()

// initialization, etc.
创建要添加到TableLayout的元素,一个带有TextView和按钮的TableRow,然后在将其添加到TableLayout之前调用addDeleteClickyourButton和uniqueTag

// example of adding a text view and a button the the TableLayout
void addToTableLayout(String text, String uniqueTag) {
    TableRow tr = new TableRow(yourActivity);
    // set the unique tag that will be used when deleting this row
    tr.setTag(uniqueTag);
    // do what you need with the button and textview
    TextView tv = new TextView(yourActivity);
    tv.setText(text);
    Button bt = new Button(yourActivity);
    // add delete click capability to the button
    addDeleteClick(bt, uniqueTag);
    table.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}

// Adds the delete on click capability to a button 
// to delete a row from TableLayout based on its tag
void addDeleteClick(Button bt, final String tag) {
    bt.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Log.d("TableLayout", " deleting row with tag " + tag);
            deleteRow(tag);
        }

    });
}

// delete a row from TableLayout based on its tag
void deleteRow(String tag) {
    View removedRow = table.findViewWithTag(tag);
    table.removeView(removedRow);
    table.invalidate();
}

有人能帮我解决这个问题吗?请参考以下对我来说很有效的内容。目前,我的设计已更改,我正在将Textview和checkbox作为一行动态添加到表中。在桌子外面我有一个按钮。当我单击该按钮时,如果在任何行中选中了复选框,则应删除该特定行。如何知道选中了哪个复选框?因为我需要知道它是ViewParent还是ViewGroups,所以您需要知道TableLayout句柄,并使用它的句柄可以枚举所有可用的子视图,这样您就知道了复选框。正如您所知,您可以将其设置为TableRow