java - 动态添加/删除表行

标签 java android android-tablelayout

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

生成的示例如下所示...

enter image description here

您能否建议我如何动态删除这些行?

谢谢

编辑:决定使用 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;
}

}

最佳答案

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

或者如果您知道索引

edits.remove(i);

关于java - 动态添加/删除表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19812586/

相关文章:

java - ServerSocket 同时不会中断图像数据输入流

android - 键盘弹起时调整 ScrollView

android - 如何制作可滚动的 TableLayout?

java - 如何修复android中的ArithmeticException错误

android - 如何从膨胀的布局中获取 View 的引用

java - JDBC 不返回结果集

java - 如何通过我的应用程序将事件设置到设备日历?

java - 调用 pack() JPOS 时获取 NullPointerException

java - 使用过滤器后如何获取特定列的值

java - 发送电子邮件 : Problem when connecting to host