【问题标题】:How do I limit the dynamically created views to 10?如何将动态创建的视图限制为 10 个?
【发布时间】:2015-11-18 17:52:55
【问题描述】:

我正在向表格布局动态添加行。我想将视图限制为 10 个视图,即仅显示 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 我充气一排(视图)。我不希望用户将视图膨胀超过 10 次。但是每次单击按钮时,只会膨胀一行(视图)。

【问题讨论】:

  • 您想要一次点击 10 个结果,还是每次点击一个结果最多 10 个点击?
  • @mjp66 每次点击一个结果,最多点击 10 次
  • 好的,然后只需设置一个计数器(int counter = 0)并为每个 onClick() 推进计数器值,并在执行操作之前检查计数器值...如果它
  • @mjp66 你能举个例子吗?我是编程新手。谢谢
  • @ArthurKorchagin 刚刚检查了您的个人资料,请您帮忙!谢谢

标签: java android views tablelayout tablerow


【解决方案1】:
yourButton.setOnClickListener(new View.OnClickListener() {
    int count = 0;
    public void onClick(View v) {
        if (count++ < 10) {
            showItem();
        }
    }
});

【讨论】:

  • 非常感谢@mjp66 :)
  • 欢迎和愉快的编程;)
猜你喜欢
  • 2015-05-02
  • 1970-01-01
  • 1970-01-01
  • 2012-05-18
  • 1970-01-01
  • 2016-05-31
  • 2016-05-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多