在我的活动中,我有一个 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;
}
}