所有json对象都会在文本更改时更新?

时间:2015-12-09 11:11:26

标签: java android json textwatcher

  

这是我的第一次文字更改日志

[{
    "custInfo": "Anup 8600931386",
    "rate": "24032",
    "weight": "21.00000",
    "makingAmt": "232",
    "description": "GENTS ANGTHI 22k NO STONE",
    "sum_total": "50954.4",
    "vat": "",
    "itemTotal": "50954.4",
    "barcode": "BQSP78BB",
    "net_rate": "24264.0",
    "date": "09-12-2015",
    "invoiceNo": "1",
    "bill_type": "Estimate"
}]
  

现在当我再添加一个条目时,前一个项目也会更新。这是第二个条目后的日志。

[{
    "custInfo": "Anup 8600931386",
    "rate": "24052",
    "weight": "12.00000",
    "makingAmt": "228",
    "description": "LADIES TOP 22K NO STONE",
    "sum_total": "29136.0",
    "vat": "",
    "itemTotal": "29136.0",
    "barcode": "BQSP82BB",
    "net_rate": "24280.0",
    "date": "09-12-2015",
    "invoiceNo": "1",
    "bill_type": "Estimate"
}, {
    "custInfo": "Anup 8600931386",
    "rate": "24052",
    "weight": "12.00000",
    "makingAmt": "228",
    "description": "LADIES TOP 22K NO STONE",
    "sum_total": "29136.0",
    "vat": "",
    "itemTotal": "29136.0",
    "barcode": "BQSP82BB",
    "net_rate": "24280.0",
    "date": "09-12-2015",
    "invoiceNo": "1",
    "bill_type": "Estimate"
}]
  

这是我的文本观察器实现,更新值的代码位于文本changed()之后。

private TextWatcher mkAmountTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            String mkAmt = makingAmount.getText().toString();
            mk = Double.parseDouble(mkAmt);
            calculateAndShow(wt, rt, mk);

            int mid = (int) newRow.getTag()-1;
            if ((mid<0) || (mid>itemSelectedJson.length())){
                return;
            }
            try{
                itemSelectedJson.getJSONObject(mid).put("makingAmt",mkAmt);

                Log.d("MAKING_TW", itemSelectedJson.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    };
  

这是用于创建json数组的代码。

 private void createJsonArray() {
    billType = (invEstSwitch.isChecked() ? textViewEstimate : textViewInvoice)
            .getText().toString();
    String invNumber = textViewInvNo.getText().toString();
    String bcode = barCode.getText().toString();
    String description = itemDesc.getText().toString();
    String wt = weightLine.getText().toString();
    String rateAmt = rateAmount.getText().toString();
    String making = makingAmount.getText().toString();
    String netr = netRate.getText().toString();
    String iTotal = itemtotal.getText().toString();
    String vatAmt = textViewVat.getText().toString();
    String sumAmt = textViewSum.getText().toString();
    String crtDate = textViewCurrentDate.getText().toString();
    try {
        jsonObject.put("custInfo", custSelected.toString());
        jsonObject.put("invoiceNo", invNumber);
        jsonObject.put("barcode", bcode);
        jsonObject.put("description", description);
        jsonObject.put("weight", wt);
        jsonObject.put("rate", rateAmt);
        jsonObject.put("makingAmt", making);
        jsonObject.put("net_rate", netr);
        jsonObject.put("itemTotal", iTotal);
        jsonObject.put("vat", vatAmt);
        jsonObject.put("sum_total", sumAmt);
        jsonObject.put("bill_type", billType);
        jsonObject.put("date", crtDate);


    } catch (JSONException e) {
        e.printStackTrace();
    }
    try {

        itemSelectedJson.put(index, jsonObject);
        index++;
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

在此方法createJsonArray()中,每次都不使用相同的JSONObject。这就是为什么你一次又一次地获得相同内容的原因。

首先尝试在jsonObject方法中初始化createJsonArray(),然后我想,它会起作用。