gpt4 book ai didi

Android Studio - 以编程方式删除表格行

转载 作者:搜寻专家 更新时间:2023-11-01 09:34:20 26 4
gpt4 key购买 nike

我已经从数组列表中动态创建了一个表

    for (int i = 0; i < arrayList.size(); i++) {
TextView name = new TextView(this);
TextView time = new TextView(this);
TableRow row = new TableRow(this);

// Set event name and remaining time
name.setText(arrayList.get(i).name);
time.setText("...");

// Add text views to row
row.addView(time, layoutParams);
row.addView(name, layoutParams);

// Add row to table
tableLayout.addView(row, i);
}

现在,如果我想在某个条件下编辑或删除某些行,我将必须访问该行,但如何访问?

    outerloop:
for(int i = 0; i < arrayList.size(); i++ ) {
if(// row's time <= 0 ...) {
tableLayout.removeViewAt(i); // this doesn't seem to work properly
// it removes the wrong rows
}
else {
// ...
break outerloop;
}
}

出于某种原因 tableLayout.removeViewAt(i); 删除了不正确的行。或者,我更愿意编辑该行的 name,但原则适用,我只需要找到一种方法来访问表行。有什么办法吗?

最佳答案

像这样创建你的表:

ArrayList<TableRow> rows = new ArrayList<>();
for (int i = 0; i < arrayList.size(); i++) {
TextView name = new TextView(this);
TextView time = new TextView(this);
TableRow row = new TableRow(this);
rows.add(row);

// Set event name and remaining time
name.setText(arrayList.get(i).name);
time.setText("...");

// Add text views to row
row.addView(time, layoutParams);
row.addView(name, layoutParams);

// Add row to table
tableLayout.addView(row, i);
}

现在当你想删除一行时,你可以通过它在 ArrayList 中的索引来实现:

row = rows.get(i);
table.removeView(row);
rows.remove(i);

当您想删除多行时,这可能是适合您的解决方案:

ArrayList<TableRow> removeRows = new ArrayList<>();
for(int i = 0; i < rows.size(); i++) {
if(....) {
removeRows.add(rows.get(i));
}
}

// now delete those rows
for(TableRow remove : removeRows) {
tableLayout.removeView(row);
rows.remove(remove);
}

我希望这对你有用!

关于Android Studio - 以编程方式删除表格行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44646014/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com