how to delete table row in table layout in android
Asked Answered
C

8

16
void init()
{
   intcolumnwidth1 = int_scr_wd*55;
   intcolumnwidth1 = intcolumnwidth1/100;
   for (int i = 0; i < strarr.length-1; i++)
   {
      strinarr = fun1.split(strarr[i].trim(),"^");
      tr1 = (TableRow) new TableRow(this);
      txt1=new TextView(this);
      txt1.setWidth(intcolumnwidth1);
      txt1.setText(strinarr[0]);
      tr1.addView(txt1);
      tl.addView(tr1,new TableLayout.LayoutParams(layoutParams));
   }
}

Scenario is this when for the first i open this page it dynamically adds rows in the table layout ... but if after some time data in database changes ... when i click on refresh button it appends the new data after the old data in the table layout... all i need is the solution for how to refresh or delete textview that already exists in the table layout...

thanx..

Cobia answered 5/4, 2011 at 14:31 Comment(0)
R
18

Try removeView

row = (TableRow)findViewById(R.id.row);
table.removeView(row);
Ridicule answered 5/4, 2011 at 14:35 Comment(1)
I think it's highly unlikely that the rows will have static ids if they are added dynamically.Grindlay
U
18

This method I have written removes all rows except the first one. It is useful if you don't want to remove the header row, for example:

private void cleanTable(TableLayout table) {

    int childCount = table.getChildCount();

    // Remove all rows except the first one
    if (childCount > 1) {
        table.removeViews(1, childCount - 1);
    }
}
Ugh answered 12/5, 2014 at 7:32 Comment(0)
C
15

I realize this is a old thread but I as shocked when I came across this without a decent answer.

I do it like this:

TableLayout table = (TableLayout) findViewById(R.id.myTable);       
table.removeAllViews();

That will remove all of the child views, in this case rows and everything. It doesn't seem by your description that you want to delete just one row, but that you want to remove them and then load the rows again. The above code will do the removing part.

Cayenne answered 15/6, 2011 at 23:40 Comment(0)
G
6

The other solutions require your rows to have unique ids.

If they don't have unique ids then how about using:

tl.removeView(rowIndex);

In any case, you should try learning how to use SimpleCursorAdapter or CursorAdapter because they are specifically designed for displaying the content of a database query in a list. See Binding to Data with AdapterView.

Grindlay answered 5/4, 2011 at 14:46 Comment(0)
L
2

You need to know the textview to be deleted. The TextView can be identified by setting a unique id using setId() or using a unique tag using setTag().

It can then be identified by TextView tv = (TextView) tr1.findViewByTag(unique tag);

Use removeView to delete the View.

Lumberman answered 5/4, 2011 at 14:39 Comment(0)
R
2

I use this:

tl.removeView(tl.getChildAt(index));
Retene answered 13/5, 2016 at 14:30 Comment(1)
Add some explanation with answer for how this answer help OP in fixing current issueMemorialize
A
1

look this code to remove row in table

private List<View> listViewRow;

if (listViewRow.size() > 0) {
    for (View view : listViewRow) {
        table.removeView(view);
    }
}

for (int i = 0; i < myList.size(); i++) {
    row.addView((new TextView(context).setText("Teste")), 0);
    row.addView((new TextView(context).setText("Teste")), 1);
    row.addView((new TextView(context).setText("Teste")), 2);

    listViewRow.add(row);
    table.addView(row, 1);
}
Arcograph answered 5/9, 2012 at 12:56 Comment(0)
B
0

Use this:

new Handler().postDelayed(new Runnable() {
      public void run() {
         TableRow row = (TableRow)table.getChildAt(i);
         table.removeView(row);
      }
 }, 100);

Where i - is row position, wich you want to delete.

Bulganin answered 14/2, 2013 at 8:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.