如何动态地删除TableLayout行 [英] How to delete a row in TableLayout dynamically

查看:143
本文介绍了如何动态地删除TableLayout行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TableLayout为我添加的行dynamically.In每一行的有2个因素,其中之一就是TextView的另一个Button.When我按一下按钮是present成一排,该行应deleted.How可以这样在Android中做了什么?如何找到的rowid以及如何动态删除一行。 谁能帮助我在设法解决这个问题。

I am having a TableLayout for which I added rows dynamically.In each of the row there are 2 elements of which one is TextView other is Button.When I click the button that is present in a row,that row should be deleted.How can this be done in Android ? How to find rowid and how to delete a row dynamically. can anyone help me in sorting out this issue.

在此先感谢,

推荐答案

您可以在您添加行指定的标签或标识的。然后,只需使用该标记/ ID删除该行。

You can assign tags or id's when you adding a row. Then just use that tag/id to delete that row.

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

// initialization, etc.

创建将被添加到TableLayout,与TextView的和按钮通过tablerow然后调用元素 addDeleteClick(yourButton,uniqueTag)将其添加到TableLayout前。

Create element that will be added to the TableLayout, a TableRow with the TextView and the Button then call addDeleteClick(yourButton, uniqueTag) before adding it to the TableLayout.

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

这篇关于如何动态地删除TableLayout行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆