1

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

}

4

2 回答 2

0
itemNames.remove(txtItemName);
edits.remove(edtItemQty);

或者如果你知道索引

edits.remove(i);
于 2013-11-06T18:03:53.150 回答
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);
        }
    };
于 2013-11-07T05:47:59.923 回答