Delete TableRow from layout dynamically



  • I want to remove the tableRow from the table, but somehow all the lines are removed, not one. I suspect why, but I don't know how to do it. Tried to remove it, permanently null pointer. And if I use removeView(tableRow), it removes all the lines, not one.

    for (Entry<String, User> entry : users_.entrySet()) {
        item = factory.inflate(R.layout.row_table, null);
        tableRow = (TableRow) item.findViewById(R.id.TableRow1);
        tableRow.setClickable(true);
        tableLayout.addView(item, new TableLayout.LayoutParams(
                                TableLayout.LayoutParams.FILL_PARENT,
                                TableLayout.LayoutParams.WRAP_CONTENT));
                tableRow.setOnLongClickListener(new OnLongClickListener() {
    
                        @Override
                        public boolean onLongClick(View v) {
                            // TODO Auto-generated method stub
                            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();
                            return false;
                        }
                    });
    }
    



  • All you have to do is find the right number of the line to be removed. It's just gonna take you away. child Ooh. TableLayout :

     private void removeRow(int index){
            int count = tl.getChildCount();
            if (count >= index) {
                    tl.removeViewAt(index);
            }
        }
    

Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2