簡體   English   中英

如何在Android中刪除表格布局中的表格行

[英]how to delete table row in table layout in android

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

場景是這種情況,當我第一次打開此頁面時,它會動態地在表布局中添加行...但是如果一段時間后數據庫中的數據發生了變化...當我單擊刷新按鈕時,它將在新數據添加到舊數據之后表格布局...我所需要的就是如何刷新或刪除表格布局中已經存在的textview的解決方案...

謝謝

嘗試removeView

row = (TableRow)findViewById(R.id.row);
table.removeView(row);

我編寫的此方法會刪除除第一個行以外的所有行。 如果您不想刪除標題行,則非常有用,例如:

private void cleanTable(TableLayout table) {

    int childCount = table.getChildCount();

    // Remove all rows except the first one
    if (childCount > 1) {
        table.removeViews(1, childCount - 1);
    }
}

我意識到這是一個古老的話題,但是當我遇到這個問題並沒有一個好的答案時,我感到非常震驚。

我這樣做是這樣的:

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

這將刪除所有子視圖,在本例中為行和所有內容。 根據您的描述,您似乎並不想刪除僅一行,而是要刪除它們然后再次加載行。 上面的代碼將刪除部分。

其他解決方案要求您的行具有唯一的ID。

如果他們沒有唯一的ID,那么如何使用:

tl.removeView(rowIndex);

無論如何,您應該嘗試學習如何使用SimpleCursorAdapterCursorAdapter,因為它們是專門為在列表中顯示數據庫查詢的內容而設計的。 請參閱使用AdapterView綁定到數據

我用這個:

tl.removeView(tl.getChildAt(index));

您需要知道要刪除的textview。 可以通過使用setId()設置唯一ID或使用setTag()使用唯一標簽來標識TextView。

然后可以通過TextView tv =(TextView)tr1.findViewByTag( unique tag );來標識它。

使用removeView刪除視圖。

看這段代碼以刪除表中的行

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

用這個:

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

i-是行位置,要刪除的位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM