【问题标题】:Adding / Removing TableRows Dynamically动态添加/删除 TableRows
【发布时间】:2013-11-06 12:50:30
【问题描述】:

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

}

【问题讨论】:

  • 确定您需要删除的内容,在表格中找到行所在的位置,然后将它们从表格中删除。
  • 您是否有任何特定需要使用TableRow 结构而不是List
  • @RaviBhatt 我添加了一个屏幕截图,向您展示我希望表格看起来像什么的示例......希望这会有所帮助
  • @LouisEvans 你想在哪个事件上删除该行?
  • @RaviBhatt 我不确定...我在想也许在同一行上有一个“X”按钮?但我不知道如何使用按钮删除行。

标签: java android android-tablelayout


【解决方案1】:
itemNames.remove(txtItemName);
edits.remove(edtItemQty);

或者如果你知道索引

edits.remove(i);

【讨论】:

    【解决方案2】:

    我建议您在将其添加到主布局时为您的行和“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);
            }
        };
    

    【讨论】:

    • 仅供参考,您也可以设置关闭按钮的标签,如“closeButton1”,但在这种情况下,您需要将文本“closeButton”和“1”分开以获取按下的按钮。
    • 感谢您的回复...但是,我无法使用 finViewByTag()。它说“R.layout 类型的 findViewByTag(Object) 方法未定义”
    • @LouisEvans 抱歉,方法是 findViewWithTag 而不是 findViewByTag。已经用它编辑了答案检查。
    • @LouisEvans 你发现成功还是有什么问题?
    • 我做的有点不同,但现在可以了,谢谢!我不知道你可以使用标签和 ID,所以这是我学到的有用的东西
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多