在我的活动中,我有一个TableLayout动态添加TableRows。
这是在for循环中完成的,如此......
for(i = 0; i < orderArray.length(); i++)
{
getSaleData(i);
//build the TextView's for each row...
txtItemName = new TextView(this);
itemNames.add(txtItemName);
txtItemName.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT, 2f));
txtItemName.setTextSize(15);
txtItemName.setMaxLines(1);
txtItemName.setId(i);
txtItemName.setEllipsize(TextUtils.TruncateAt.END);
edtItemQty = new EditText(this);
edits.add(edtItemQty);
edtItemQty.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f));
edtItemQty.setTextSize(15);
edtItemQty.setInputType(InputType.TYPE_CLASS_NUMBER);
itemNames.get(i).setText(currentItem);
edits.get(i).setText(itemQuantity);
//alternate between line colors depending on the flag
if(lineColor)
{
itemNames.get(i).setBackgroundColor(Color.parseColor(LINE_COLOR));
edits.get(i).setBackgroundColor(Color.parseColor(LINE_COLOR));
lineColor = false;
}
else
{
lineColor = true;
}
//create a new TableRow and add the TextViews
TableRow row = new TableRow(this);
row.addView(itemNames.get(i));
row.addView(edits.get(i));
//add row to the table layout
tblItems.addView(row, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
结果示例如下所示......
请问您如何动态删除这些行?
由于
编辑:决定使用longClickListener
以下是我最终使用的完整代码...
longClickListener = new View.OnLongClickListener()
{
@Override
public boolean onLongClick(View v)
{
//get the row
final TableRow row = (TableRow)tblItems.findViewWithTag(v.getTag());
//confirm the deletion first...
final AlertDialog.Builder confirm = new AlertDialog.Builder(TransactionEdit.this);
confirm.setIcon(R.drawable.warningicon);
confirm.setTitle("Remove Item?");
confirm.setMessage("Remove this item from the the order?");
confirm.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
//find the matching row in the List
for(int j = 0; j < edits.size(); j ++)
{
//tags contain a prefix and a number e.g. qty5 (Same with rows)
String qtyIndex = edits.get(j).getTag().toString().replace("qty", "");
String rowIndex = row.getTag().toString().replace("row", "");
//if its the same one...
if(qtyIndex.equals(rowIndex))
{
//...set the text to 0 to flag as not to be include...
edits.get(j).setText((CharSequence)"0");
break;
}
}
//...then hide the row
row.setVisibility(View.GONE);
}
});
confirm.setNegativeButton("Cancel", null);
confirm.show();
return false;
}
}
答案 0 :(得分:0)
itemNames.remove(txtItemName);
edits.remove(edtItemQty);
或者你知道索引
edits.remove(i);
答案 1 :(得分:0)
我建议您在将行添加到主布局时为行和“x”按钮运行时添加标记。
您提供的'id'应该相对于表行的“row1”和第1行的'x'按钮的“1”。您在此处使用循环,因此行row.setTag("row"+i)
和closeButton.setTag(i)
会这样做的。
为'x'按钮创建一个公共监听器,并获取它的标签。从标记中过滤数字,并删除或删除具有相同标记号id的行的可见性。
有些事情:
private View.OnClickListener commonListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Button closeButton = (Button) v;
TableRow r = (TableRow)layout.findViewWithTag("row"+closeButton.getTag());
r.setVisibility(View.GONE);
}
};