java - 如何将动态创建的 View 限制为 10 个?

标签 java android view tablelayout tablerow

我正在向表格布局动态添加行。我想将 View 限制为 10 个 View ,即仅显示 10 行。

OnClick 我调用一个名为 showItem() 的方法。

    private void showItem(String json) {
    String itembarcode = "";
    String itemdesc = "";
    String weight = "";
    String rate = "";
    String making = "";
    String netrate = "";
    String total = "";


    try {
        JSONObject jsonObject = new JSONObject(json);
        JSONArray result = jsonObject.getJSONArray(ParseBarcode.JSON_ARRAY);
        JSONObject itemData = result.getJSONObject(0);
        itembarcode = itemData.getString(ParseBarcode.KEY_BARCODE);
        itemdesc = itemData.getString(ParseBarcode.KEY_DESC);
        weight = itemData.getString(ParseBarcode.KEY_WEIGHT);
        rate = itemData.getString(ParseBarcode.KEY_RATE);
        making = itemData.getString(ParseBarcode.KEY_MAKING);
        netrate = itemData.getString(ParseBarcode.KEY_NETRATE);
        total = itemData.getString(ParseBarcode.KEY_TOTAL);
    } catch (JSONException e) {
        e.printStackTrace();
    }


    //table started

    TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
    TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1f);
    rowParams.setMargins(16, 0, 16, 0);

    TableLayout tableLayout = new TableLayout(AddInvEst.this);
    tableLayout.setLayoutParams(tableParams);

    TableRow newRow = new TableRow(AddInvEst.this);
    newRow.setLayoutParams(tableParams);

    barCode = new TextView(AddInvEst.this);
    barCode.setLayoutParams(rowParams);
    barCode.setGravity(Gravity.CENTER);

    itemDesc = new TextView(AddInvEst.this);
    itemDesc.setLayoutParams(rowParams);
    itemDesc.setGravity(Gravity.CENTER);

    weightLine = new TextView(AddInvEst.this);
    weightLine.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.75f));
    weightLine.setGravity(Gravity.CENTER);

    rateAmount = new EditText(AddInvEst.this);
    rateAmount.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.5f));
    rateAmount.setGravity(Gravity.CENTER);
    rateAmount.addTextChangedListener(rateTextWatcher);

    makingAmount = new EditText(AddInvEst.this);
    makingAmount.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.5f));
    makingAmount.setGravity(Gravity.CENTER);
    makingAmount.addTextChangedListener(mkAmountTextWatcher);

    netRate = new TextView(AddInvEst.this);
    netRate.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.5f));
    netRate.setGravity(Gravity.CENTER);
    netrates.add(netrate);

    itemtotal = new TextView(AddInvEst.this);
    itemtotal.setLayoutParams(rowParams);
    itemtotal.setGravity(Gravity.CENTER);
    totals.add(total);

    double[] doubleList = new double[totals.size()];
    double sum = 0;
    for (int i = 0; i < totals.size(); ++i) {
        doubleList[i] = Double.parseDouble(totals.get(i));
        sum += doubleList[i];
    }

    barCode.setText(itembarcode);
    itemDesc.setText(itemdesc);
    weightLine.setText(weight);
    rateAmount.setText(rate);
    makingAmount.setText(making);
    netRate.setText(netrate);
    itemtotal.setText(total);
    textViewSum.setText(sum * (0.02) + sum + "");//set total text to sum
    textViewVat.setText(sum * (0.02) + "");

    newRow.addView(barCode);
    newRow.addView(itemDesc);
    newRow.addView(weightLine);
    newRow.addView(rateAmount);
    newRow.addView(makingAmount);
    newRow.addView(netRate);
    newRow.addView(itemtotal);
    itemTable.addView(newRow);
}

现在onClick我膨胀了一行( View )。我不希望用户将 View 膨胀超过 10 次。但是每次单击按钮时,只有一行( View )会膨胀。

最佳答案

yourButton.setOnClickListener(new View.OnClickListener() {
    int count = 0;
    public void onClick(View v) {
        if (count++ < 10) {
            showItem();
        }
    }
});

关于java - 如何将动态创建的 View 限制为 10 个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33786957/

相关文章:

java - 无法修复错误 : Cannot resolve symbol 'OnClickListener'

java - 在 Java 中访问辅助类的其他方法

java - FindViewById 循环

Android谷歌地图自定义布局

objective-c - 在 IB 中,如何将现有组件放置在新的 SplitView 中?

java 小服务程序 : request parameter contains plus

java - 在外部文件夹 war 文件命令中运行类路径 Spring java

java - servlet url 模式匹配实现错误

java - Android Heads Up 通知未显示

android - TYPE_ROTATION_VECTOR 的传感器值在不同设备上不相同