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

标签 java android json textwatcher

This is my log for the 1st text change

[{
    "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"
}]

Now when I add one more entry the previous item also gets updated.This is the log after the 2nd entry.

[{
    "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"
}]

This is my text watcher implementation and the code that updates the values is inside after text 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();
            }
        }
    };

This is the code for creating the json array.

 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();
    }
}

最佳答案

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

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

关于java - 所有 json 对象都会在文本更改时更新吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34177347/

相关文章:

java - 获取 Android 库中源代码的校验和

javascript - 为什么 JSON.parse() 会抛出 Uncaught SyntaxError : Unexpected token when trying to parse a simple json object?

java - 强制用户仅添加希伯来字母

java - XWPF POI 如何在没有自动换行的情况下设置段落中的文本

android - 圆形进度条未覆盖整个可用空间

android - PendingIntent.getBroadcast 无法解析

java - 获取 Java.sql.SQLException : ORA-01461: can bind a LONG value only for insert into a LONG column. 。插入数据时

java - 使用 url 防止重定向

java - 如何编写使用 XML 的测试用例?

javascript - 在 JavaScript 和 JSON 中,为什么 str = JSON.stringify(obj); 的结果是等于 {}?