Advertisement
AnupBorde

Updating json array

Dec 8th, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.43 KB | None | 0 0
  1. //This is the code for text watcher
  2. private void calculateAndShow(double wt, double rt, double mk) {
  3. String w = weightLine.getText().toString();
  4. wt = Double.parseDouble(w);
  5. double NetRate = (double) Math.round((rt + mk) * 100.0) / 100.0;
  6. double Total = (double) Math.round(((NetRate / 10) * wt) * 100.0) / 100.0;
  7.  
  8. netRate.setText(NetRate + "");
  9. itemtotal.setText(Total + "");
  10. }
  11.  
  12. private TextWatcher rateTextWatcher = new TextWatcher() {
  13. @Override
  14. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  15. }
  16.  
  17. @Override
  18. public void onTextChanged(CharSequence s, int start, int before, int count) {
  19. }
  20.  
  21. @Override
  22. public void afterTextChanged(Editable s) {
  23. String rate = rateAmount.getText().toString();
  24. rt = Double.parseDouble(rate);
  25. calculateAndShow(wt, rt, mk);
  26. rates.add(rate);
  27.  
  28. int rid = (int) newRow.getTag()-1;
  29. if ((rid<0) || (rid>itemSelectedJson.length())){
  30. return;
  31. }
  32. try {
  33. itemSelectedJson.getJSONObject(rid).put("rate",rate);
  34. Log.d("RATE_TW", itemSelectedJson.toString());
  35. } catch (JSONException e) {
  36. e.printStackTrace();
  37. }
  38.  
  39.  
  40. // for (int i=0; i< itemSelectedJson.length(); i++ )
  41. // {
  42. // try {
  43. // itemSelectedJson.getJSONObject(i).put("rate",rate);
  44. // Log.d("RATE_TW", itemSelectedJson.toString());
  45. // } catch (JSONException e) {
  46. // e.printStackTrace();
  47. // }
  48. // }
  49.  
  50. }
  51. };
  52.  
  53. private TextWatcher mkAmountTextWatcher = new TextWatcher() {
  54. @Override
  55. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  56. }
  57.  
  58. @Override
  59. public void onTextChanged(CharSequence s, int start, int before, int count) {
  60. }
  61.  
  62. @Override
  63. public void afterTextChanged(Editable s) {
  64. String mkAmt = makingAmount.getText().toString();
  65. mk = Double.parseDouble(mkAmt);
  66. calculateAndShow(wt, rt, mk);
  67. mkCharges.add(mkAmt);
  68.  
  69. int mid = (int) newRow.getTag()-1;
  70. if ((mid<0) || (mid>itemSelectedJson.length())){
  71. return;
  72. }
  73. try{
  74. itemSelectedJson.getJSONObject(mid).put("makingAmt",mkAmt);
  75. Log.d("MAKING_TW", itemSelectedJson.toString());
  76. } catch (JSONException e) {
  77. e.printStackTrace();
  78. }
  79.  
  80. // for (int i=0; i< itemSelectedJson.length(); i++)
  81. // {
  82. // try{
  83. // itemSelectedJson.getJSONObject(i).put("makingAmt",mkAmt);
  84. // Log.d("MAKING_TW", itemSelectedJson.toString());
  85. // } catch (JSONException e) {
  86. // e.printStackTrace();
  87. // }
  88. // }
  89. }
  90. };
  91.  
  92. //Text watcher for net rate
  93. private TextWatcher netrateTextWatcher = new TextWatcher() {
  94. @Override
  95. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  96. }
  97.  
  98. @Override
  99. public void onTextChanged(CharSequence s, int start, int before, int count) {
  100. }
  101.  
  102. @Override
  103. public void afterTextChanged(Editable s) {
  104. String netChange = netRate.getText().toString();
  105. final int row_id = (int) newRow.getTag();
  106.  
  107. if ((row_id<0) || (row_id> itemSelectedJson.length())){
  108. return;
  109. }
  110.  
  111. try {
  112. itemSelectedJson.getJSONObject(row_id-1).put("net_rate",netChange);
  113. Log.d("NETRATE_TW",itemSelectedJson.toString());
  114. } catch (JSONException e) {
  115. e.printStackTrace();
  116. }
  117.  
  118. // for (int i=0; i<itemSelectedJson.length();i++)
  119. // {
  120. // try {
  121. // itemSelectedJson.getJSONObject(i).put("net_rate",netChange);
  122. // Log.d("NETRATE_TW",itemSelectedJson.toString());
  123. // } catch (JSONException e) {
  124. // e.printStackTrace();
  125. // }
  126. // }
  127. }
  128. };
  129.  
  130. //Text watcher for sum total of all selected items
  131. private TextWatcher totalTextWatcher = new TextWatcher() {
  132. @Override
  133. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  134. }
  135.  
  136. @Override
  137. public void onTextChanged(CharSequence s, int start, int before, int count) {
  138. }
  139.  
  140. @Override
  141. public void afterTextChanged(final Editable editable) {
  142. //for text change in textview itemTotal
  143. String total_prize = itemtotal.getText().toString();
  144. final int row_id = (int) newRow.getTag();
  145.  
  146. if ((row_id<0) || (row_id> itemSelectedJson.length())){
  147. return;
  148. }
  149.  
  150. try {
  151. itemSelectedJson.getJSONObject(row_id-1).put("itemTotal",total_prize);
  152. Log.d("TOTAL_TW",itemSelectedJson.toString());
  153. } catch (JSONException e) {
  154. e.printStackTrace();
  155. }
  156.  
  157.  
  158. final int id = (int) itemtotal.getTag();
  159.  
  160.  
  161. if ((id < 0) || (id > totals.size())) {
  162. return;
  163. }
  164. final String itemTotalValue = itemtotal.getText().toString();
  165. totals.set(id - 1, itemTotalValue);
  166.  
  167. double newSum = 0.0d;
  168.  
  169. for (int i = 0; i < totals.size(); ++i) {
  170. final double value = Double.parseDouble(totals.get(i));
  171. newSum += Math.round(value * 100) / 100.0;
  172. }
  173.  
  174. if (invEstSwitch.isChecked()) {
  175. final double estSum = Math.round((newSum) * 100.0) / 100.0;
  176. textViewSum.setText("Rs." + Double.toString(estSum));//set total text to sum
  177. textViewVat.setText("");
  178. textViewVatText.setText("");
  179. } else {
  180. final double sum = Math.round((newSum * 0.02 + newSum) * 100.0) / 100.0;
  181. textViewSum.setText("Rs." + Double.toString(sum));//set total text to sum
  182.  
  183. final double vat = Math.round((newSum * 0.02) * 100.0) / 100.0;
  184. textViewVat.setText("Rs." + Double.toString(vat));
  185. }
  186.  
  187. //for textview itemTotalValue
  188. for (int i=0; i<itemSelectedJson.length(); i++)
  189. {
  190. try {
  191. itemSelectedJson.getJSONObject(i).put("sum_total", itemTotalValue);
  192. Log.d("SUMTOTAL_TW", itemSelectedJson.toString());
  193. // display.setText(itemSelectedJson.toString());
  194. } catch (JSONException e) {
  195. e.printStackTrace();
  196. }
  197. }
  198. }
  199. };
  200. //This is the dynamically created table rows where I have set the text change listener
  201. private void itemTable(String itembarcode, String itemdesc, String weight, String rate, String making, String netrate, String total) {
  202. tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
  203. rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1f);
  204. rowParams.setMargins(16, 0, 16, 0);
  205.  
  206. tableLayout = new TableLayout(AddInvEst.this);
  207. tableLayout.setLayoutParams(tableParams);
  208.  
  209. newRow = new TableRow(AddInvEst.this);
  210. newRow.setLayoutParams(tableParams);
  211. newRow.setTag(count);
  212. Toast.makeText(AddInvEst.this, "The Row Number is "+newRow.getTag().toString(), Toast.LENGTH_SHORT).show();
  213.  
  214. barCode = new TextView(AddInvEst.this);
  215. barCode.setLayoutParams(rowParams);
  216. barCode.setGravity(Gravity.CENTER);
  217.  
  218. itemDesc = new TextView(AddInvEst.this);
  219. itemDesc.setLayoutParams(rowParams);
  220. itemDesc.setGravity(Gravity.CENTER);
  221.  
  222. weightLine = new TextView(AddInvEst.this);
  223. weightLine.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.75f));
  224. weightLine.setGravity(Gravity.CENTER);
  225.  
  226. rateAmount = new EditText(AddInvEst.this);
  227. rateAmount.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.5f));
  228. rateAmount.setGravity(Gravity.CENTER);
  229. rateAmount.setInputType(InputType.TYPE_CLASS_PHONE);
  230. rateAmount.setTag(count);
  231. rateAmount.addTextChangedListener(rateTextWatcher);
  232.  
  233. makingAmount = new EditText(AddInvEst.this);
  234. makingAmount.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.5f));
  235. makingAmount.setGravity(Gravity.CENTER);
  236. makingAmount.setInputType(InputType.TYPE_CLASS_PHONE);
  237. makingAmount.setTag(count);
  238. makingAmount.addTextChangedListener(mkAmountTextWatcher);
  239.  
  240. netRate = new TextView(AddInvEst.this);
  241. netRate.setTag(count);
  242. netRate.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.5f));
  243. netRate.setGravity(Gravity.CENTER);
  244. netrates.add(netrate);
  245. netRate.addTextChangedListener(netrateTextWatcher);
  246.  
  247. itemtotal = new TextView(AddInvEst.this);
  248. itemtotal.setTag(count);
  249. itemtotal.setLayoutParams(rowParams);
  250. itemtotal.setGravity(Gravity.CENTER);
  251. Toast.makeText(AddInvEst.this, "item count is " + itemtotal.getTag(), Toast.LENGTH_SHORT).show();
  252. totals.add(total);
  253. itemtotal.addTextChangedListener(totalTextWatcher);
  254.  
  255. double sum = 0.0d;
  256. for (int i = 0; i < totals.size(); ++i) {
  257. final double sumvalue = Double.parseDouble(totals.get(i));
  258. sum += Math.round(sumvalue * 100) / 100.0;
  259. }
  260.  
  261. barCode.setText(itembarcode);
  262. itemDesc.setText(itemdesc);
  263. weightLine.setText(weight);
  264. rateAmount.setText(rate);
  265. makingAmount.setText(making);
  266. netRate.setText(netrate);
  267. itemtotal.setText(total);
  268.  
  269. if (invEstSwitch.isChecked()) {
  270. final double estimated_sum = Math.round((sum) * 100.0) / 100.0;
  271. textViewSum.setText("RS." + Double.toString(estimated_sum) + "");//set total text to sum
  272. textViewVat.setText("");
  273. } else {
  274.  
  275. final double orignal_sum = Math.round((sum * 0.02 + sum) * 100.0) / 100.0;
  276. textViewSum.setText("RS." + Double.toString(orignal_sum) + "");//set total text to sum
  277.  
  278. final double original_vat = Math.round((sum * 0.02) * 100.0) / 100.0;
  279. textViewVat.setText("RS." + Double.toString(original_vat) + "");
  280. }
  281.  
  282. createJsonArray();
  283.  
  284. newRow.addView(barCode);
  285. newRow.addView(itemDesc);
  286. newRow.addView(weightLine);
  287. newRow.addView(rateAmount);
  288. newRow.addView(makingAmount);
  289. newRow.addView(netRate);
  290. newRow.addView(itemtotal);
  291. itemTable.addView(newRow);
  292.  
  293. Log.d("SELECTED_ITEMS", itemSelectedJson.toString().trim());
  294.  
  295. }
  296.  
  297. //This the code to insert and create a json array
  298.  
  299.  
  300. private void insertToDb() throws JSONException {
  301.  
  302. final String jsonArray = itemSelectedJson.toString().trim();
  303.  
  304. StringRequest stringRequest = new StringRequest(Request.Method.POST, INVEST_URL,
  305. new Response.Listener<String>() {
  306. @Override
  307. public void onResponse(String response) {
  308. //display.setText("This is the Response : " + response);
  309.  
  310.  
  311. Toast.makeText(AddInvEst.this, "This is the response" + response, Toast.LENGTH_LONG).show();
  312. Log.d("RESPONSE", response.toString().trim());
  313. }
  314. },
  315. new Response.ErrorListener() {
  316. @Override
  317. public void onErrorResponse(VolleyError error) {
  318.  
  319. }
  320. }) {
  321. @Override
  322. protected Map<String, String> getParams() throws AuthFailureError {
  323.  
  324. Map<String, String> params = new HashMap<>();
  325. params.put(KEY_JSONARRAY, jsonArray);
  326. return params;
  327. }
  328. };
  329.  
  330. RequestQueue requestQ = Volley.newRequestQueue(this);
  331. requestQ.add(stringRequest);
  332.  
  333. }
  334.  
  335. private void createJsonArray() {
  336. billType = (invEstSwitch.isChecked() ? textViewEstimate : textViewInvoice)
  337. .getText().toString();
  338. String invNumber = textViewInvNo.getText().toString();
  339. String bcode = barCode.getText().toString();
  340. String description = itemDesc.getText().toString();
  341. String wt = weightLine.getText().toString();
  342. String rateAmt = rateAmount.getText().toString();
  343. String making = makingAmount.getText().toString();
  344. String netr = netRate.getText().toString();
  345. String iTotal = itemtotal.getText().toString();
  346. String vatAmt = textViewVat.getText().toString();
  347. String sumAmt = textViewSum.getText().toString();
  348. String crtDate = textViewCurrentDate.getText().toString();
  349. try {
  350. jsonObject.put("custInfo", custSelected.toString());
  351. jsonObject.put("invoiceNo", invNumber);
  352. jsonObject.put("barcode", bcode);
  353. jsonObject.put("description", description);
  354. jsonObject.put("weight", wt);
  355. jsonObject.put("rate", rateAmt);
  356. jsonObject.put("makingAmt", making);
  357. jsonObject.put("net_rate", netr);
  358. jsonObject.put("itemTotal", iTotal);
  359. jsonObject.put("vat", vatAmt);
  360. jsonObject.put("sum_total", sumAmt);
  361. jsonObject.put("bill_type", billType);
  362. jsonObject.put("date", crtDate);
  363.  
  364.  
  365. } catch (JSONException e) {
  366. e.printStackTrace();
  367. }
  368. try {
  369. itemSelectedJson.put(index, jsonObject);
  370. index++;
  371. } catch (JSONException e) {
  372. e.printStackTrace();
  373. }
  374. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement