我有一个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();
}
相关推荐
最新文章