Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This is the code for text watcher
- private void calculateAndShow(double wt, double rt, double mk) {
- String w = weightLine.getText().toString();
- wt = Double.parseDouble(w);
- double NetRate = (double) Math.round((rt + mk) * 100.0) / 100.0;
- double Total = (double) Math.round(((NetRate / 10) * wt) * 100.0) / 100.0;
- netRate.setText(NetRate + "");
- itemtotal.setText(Total + "");
- }
- private TextWatcher rateTextWatcher = 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 rate = rateAmount.getText().toString();
- rt = Double.parseDouble(rate);
- calculateAndShow(wt, rt, mk);
- rates.add(rate);
- int rid = (int) newRow.getTag()-1;
- if ((rid<0) || (rid>itemSelectedJson.length())){
- return;
- }
- try {
- itemSelectedJson.getJSONObject(rid).put("rate",rate);
- Log.d("RATE_TW", itemSelectedJson.toString());
- } catch (JSONException e) {
- e.printStackTrace();
- }
- // for (int i=0; i< itemSelectedJson.length(); i++ )
- // {
- // try {
- // itemSelectedJson.getJSONObject(i).put("rate",rate);
- // Log.d("RATE_TW", itemSelectedJson.toString());
- // } catch (JSONException e) {
- // e.printStackTrace();
- // }
- // }
- }
- };
- 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);
- mkCharges.add(mkAmt);
- 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();
- }
- // for (int i=0; i< itemSelectedJson.length(); i++)
- // {
- // try{
- // itemSelectedJson.getJSONObject(i).put("makingAmt",mkAmt);
- // Log.d("MAKING_TW", itemSelectedJson.toString());
- // } catch (JSONException e) {
- // e.printStackTrace();
- // }
- // }
- }
- };
- //Text watcher for net rate
- private TextWatcher netrateTextWatcher = 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 netChange = netRate.getText().toString();
- final int row_id = (int) newRow.getTag();
- if ((row_id<0) || (row_id> itemSelectedJson.length())){
- return;
- }
- try {
- itemSelectedJson.getJSONObject(row_id-1).put("net_rate",netChange);
- Log.d("NETRATE_TW",itemSelectedJson.toString());
- } catch (JSONException e) {
- e.printStackTrace();
- }
- // for (int i=0; i<itemSelectedJson.length();i++)
- // {
- // try {
- // itemSelectedJson.getJSONObject(i).put("net_rate",netChange);
- // Log.d("NETRATE_TW",itemSelectedJson.toString());
- // } catch (JSONException e) {
- // e.printStackTrace();
- // }
- // }
- }
- };
- //Text watcher for sum total of all selected items
- private TextWatcher totalTextWatcher = 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(final Editable editable) {
- //for text change in textview itemTotal
- String total_prize = itemtotal.getText().toString();
- final int row_id = (int) newRow.getTag();
- if ((row_id<0) || (row_id> itemSelectedJson.length())){
- return;
- }
- try {
- itemSelectedJson.getJSONObject(row_id-1).put("itemTotal",total_prize);
- Log.d("TOTAL_TW",itemSelectedJson.toString());
- } catch (JSONException e) {
- e.printStackTrace();
- }
- final int id = (int) itemtotal.getTag();
- if ((id < 0) || (id > totals.size())) {
- return;
- }
- final String itemTotalValue = itemtotal.getText().toString();
- totals.set(id - 1, itemTotalValue);
- double newSum = 0.0d;
- for (int i = 0; i < totals.size(); ++i) {
- final double value = Double.parseDouble(totals.get(i));
- newSum += Math.round(value * 100) / 100.0;
- }
- if (invEstSwitch.isChecked()) {
- final double estSum = Math.round((newSum) * 100.0) / 100.0;
- textViewSum.setText("Rs." + Double.toString(estSum));//set total text to sum
- textViewVat.setText("");
- textViewVatText.setText("");
- } else {
- final double sum = Math.round((newSum * 0.02 + newSum) * 100.0) / 100.0;
- textViewSum.setText("Rs." + Double.toString(sum));//set total text to sum
- final double vat = Math.round((newSum * 0.02) * 100.0) / 100.0;
- textViewVat.setText("Rs." + Double.toString(vat));
- }
- //for textview itemTotalValue
- for (int i=0; i<itemSelectedJson.length(); i++)
- {
- try {
- itemSelectedJson.getJSONObject(i).put("sum_total", itemTotalValue);
- Log.d("SUMTOTAL_TW", itemSelectedJson.toString());
- // display.setText(itemSelectedJson.toString());
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }
- };
- //This is the dynamically created table rows where I have set the text change listener
- private void itemTable(String itembarcode, String itemdesc, String weight, String rate, String making, String netrate, String total) {
- tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
- rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1f);
- rowParams.setMargins(16, 0, 16, 0);
- tableLayout = new TableLayout(AddInvEst.this);
- tableLayout.setLayoutParams(tableParams);
- newRow = new TableRow(AddInvEst.this);
- newRow.setLayoutParams(tableParams);
- newRow.setTag(count);
- Toast.makeText(AddInvEst.this, "The Row Number is "+newRow.getTag().toString(), Toast.LENGTH_SHORT).show();
- 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.setInputType(InputType.TYPE_CLASS_PHONE);
- rateAmount.setTag(count);
- 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.setInputType(InputType.TYPE_CLASS_PHONE);
- makingAmount.setTag(count);
- makingAmount.addTextChangedListener(mkAmountTextWatcher);
- netRate = new TextView(AddInvEst.this);
- netRate.setTag(count);
- netRate.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.5f));
- netRate.setGravity(Gravity.CENTER);
- netrates.add(netrate);
- netRate.addTextChangedListener(netrateTextWatcher);
- itemtotal = new TextView(AddInvEst.this);
- itemtotal.setTag(count);
- itemtotal.setLayoutParams(rowParams);
- itemtotal.setGravity(Gravity.CENTER);
- Toast.makeText(AddInvEst.this, "item count is " + itemtotal.getTag(), Toast.LENGTH_SHORT).show();
- totals.add(total);
- itemtotal.addTextChangedListener(totalTextWatcher);
- double sum = 0.0d;
- for (int i = 0; i < totals.size(); ++i) {
- final double sumvalue = Double.parseDouble(totals.get(i));
- sum += Math.round(sumvalue * 100) / 100.0;
- }
- barCode.setText(itembarcode);
- itemDesc.setText(itemdesc);
- weightLine.setText(weight);
- rateAmount.setText(rate);
- makingAmount.setText(making);
- netRate.setText(netrate);
- itemtotal.setText(total);
- if (invEstSwitch.isChecked()) {
- final double estimated_sum = Math.round((sum) * 100.0) / 100.0;
- textViewSum.setText("RS." + Double.toString(estimated_sum) + "");//set total text to sum
- textViewVat.setText("");
- } else {
- final double orignal_sum = Math.round((sum * 0.02 + sum) * 100.0) / 100.0;
- textViewSum.setText("RS." + Double.toString(orignal_sum) + "");//set total text to sum
- final double original_vat = Math.round((sum * 0.02) * 100.0) / 100.0;
- textViewVat.setText("RS." + Double.toString(original_vat) + "");
- }
- createJsonArray();
- newRow.addView(barCode);
- newRow.addView(itemDesc);
- newRow.addView(weightLine);
- newRow.addView(rateAmount);
- newRow.addView(makingAmount);
- newRow.addView(netRate);
- newRow.addView(itemtotal);
- itemTable.addView(newRow);
- Log.d("SELECTED_ITEMS", itemSelectedJson.toString().trim());
- }
- //This the code to insert and create a json array
- private void insertToDb() throws JSONException {
- final String jsonArray = itemSelectedJson.toString().trim();
- StringRequest stringRequest = new StringRequest(Request.Method.POST, INVEST_URL,
- new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- //display.setText("This is the Response : " + response);
- Toast.makeText(AddInvEst.this, "This is the response" + response, Toast.LENGTH_LONG).show();
- Log.d("RESPONSE", response.toString().trim());
- }
- },
- new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- }
- }) {
- @Override
- protected Map<String, String> getParams() throws AuthFailureError {
- Map<String, String> params = new HashMap<>();
- params.put(KEY_JSONARRAY, jsonArray);
- return params;
- }
- };
- RequestQueue requestQ = Volley.newRequestQueue(this);
- requestQ.add(stringRequest);
- }
- 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();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement