Android Open Source - cellar-communicator Summary Activity






From Project

Back to project page cellar-communicator.

License

The source code is released under:

GNU General Public License

If you think the Android project cellar-communicator listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.vinit.orderplacer;
/*from   w  w w.j av  a  2  s  .  c  om*/
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class SummaryActivity extends Activity {

  Bundle b;
  int sum;
  List<LinearLayout> rowContainers = new ArrayList<LinearLayout>();
  List<TextView> productNames = new ArrayList<TextView>();
  List<Button> inc = new ArrayList<Button>();
  List<EditText> productCount = new ArrayList<EditText>();
  List<Button> dec = new ArrayList<Button>();
  List<TextView> cost = new ArrayList<TextView>();

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(
        LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 0.5f);
    buttonParams.setMargins(40, 20, 40, 10);

    sum = 0;
    StringBuffer sb = new StringBuffer();
    Intent intent = getIntent();
    b = intent.getExtras();
    TextView tv = (TextView) findViewById(R.id.summary_text);

    Button submit, redo, refresh;

    /*
     * submit = (Button) findViewById(R.id.final_submit); redo = (Button)
     * findViewById(R.id.redo);
     */


    redo = new Button(this);
    redo.setLayoutParams(buttonParams);
    redo.setBackgroundResource(R.drawable.light_selector);
    redo.setTextColor(Color.WHITE);
    redo.setText("Add more");

    submit = new Button(this);
    submit.setLayoutParams(buttonParams);
    submit.setBackgroundResource(R.drawable.dark_selector);
    submit.setTextColor(Color.WHITE);
    submit.setText("Submit");

    submit.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        try {
          JSONArray jOutArr = new JSONArray();
          JSONObject jOutObj;
          JSONArray jArr = new JSONArray(b.getString("data"));
          JSONObject jObj;

          for (int i = 0; i < jArr.length(); i++) {
            jObj = jArr.getJSONObject(i);
            jOutObj = new JSONObject();
            jOutObj.put("object", jObj.getString("object"));
            jOutObj.put("category", jObj.get("category"));
            jOutObj.put("price", jObj.get("price"));
            String count = productCount.get(i).getText().toString();
            jOutObj.put("count", count);
            jOutArr.put(jOutObj);
          }

          Intent dump = new Intent(getApplicationContext(),
              CommentBox.class);
          dump.putExtra("data", jOutArr.toString());
          dump.putExtra("salesman", b.getString("salesman"));
          dump.putExtra("party", b.getString("party"));
          startActivity(dump);

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

      }
    });

    redo.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        Intent redoIntent = new Intent(getApplicationContext(),
            Salesman.class);
        redoIntent.putExtra("salesman", b.getString("salesman"));
        startActivity(redoIntent);
      }
    });

    ScrollView scroll = new ScrollView(this);
    scroll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
        LayoutParams.MATCH_PARENT));

    LinearLayout scroll_contain = new LinearLayout(this);
    scroll_contain.setLayoutParams(new LinearLayout.LayoutParams(
        LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    scroll_contain.setOrientation(LinearLayout.VERTICAL);

    try {

      JSONArray jArr = new JSONArray(b.getString("data"));
      JSONObject jObj;
      TextView tcost = new TextView(this);
      tcost.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
          LayoutParams.WRAP_CONTENT));
      tcost.setTextSize(20);
      tcost.setPadding(0, 10, 0, 0);

      for (int i = 0; i < jArr.length(); i++) {
        jObj = jArr.getJSONObject(i);
        sum += Integer.parseInt(jObj.getString("price"))
            * Integer.parseInt(jObj.getString("count"));
        rowContainers.add(new LinearLayout(this));
        rowContainers.get(i).setLayoutParams(
            new LinearLayout.LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));
        rowContainers.get(i).setOrientation(LinearLayout.HORIZONTAL);

        productNames.add(new TextView(this));
        productNames.get(i).setLayoutParams(
            RetrieveJSON.getBasicParams(0.6f));
        productNames.get(i).setText(jObj.getString("object"));

        productCount.add(new EditText(this));
        productCount.get(i).setLayoutParams(
            RetrieveJSON.getBasicParams(0.8f));
        productCount.get(i).setMinEms(3);
        productCount.get(i).setInputType(InputType.TYPE_CLASS_NUMBER);
        productCount.get(i).setGravity(Gravity.CENTER);
        productCount.get(i).setText(jObj.getString("count"));

        dec.add(new Button(this));
        dec.get(i).setLayoutParams(RetrieveJSON.getBasicParams(0.9f));
        dec.get(i).setText("<");
        dec.get(i).setTextColor(Color.parseColor("#0aa29c"));
        dec.get(i).setBackgroundColor(Color.TRANSPARENT);
        dec.get(i).setOnClickListener(
            new CounterOnClickListener(productCount.get(i), false,
                Integer.parseInt(jObj.getString("price")),
                tcost));

        inc.add(new Button(this));
        inc.get(i).setLayoutParams(RetrieveJSON.getBasicParams(0.9f));
        inc.get(i).setText(">");
        inc.get(i).setTextColor(Color.parseColor("#0aa29c"));
        inc.get(i).setBackgroundColor(Color.TRANSPARENT);
        inc.get(i).setOnClickListener(
            new CounterOnClickListener(productCount.get(i), true,
                Integer.parseInt(jObj.getString("price")),
                tcost));

        rowContainers.get(i).addView(productNames.get(i));
        rowContainers.get(i).addView(dec.get(i));
        rowContainers.get(i).addView(productCount.get(i));
        rowContainers.get(i).addView(inc.get(i));

        scroll_contain.addView(rowContainers.get(i));
      }
      tcost.setText("Total: " + Integer.toString(sum));

      scroll_contain.addView(tcost);
      scroll_contain.addView(redo);
      scroll_contain.addView(submit);
      scroll.addView(scroll_contain);
      LinearLayout parent = new LinearLayout(this);
      parent.setLayoutParams(new LinearLayout.LayoutParams(
          LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
      parent.addView(scroll);
      setContentView(parent);
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }

  @Override
  public void onBackPressed() {
    Intent redoIntent = new Intent(getApplicationContext(), Salesman.class);
    redoIntent.putExtra("salesman", b.getString("salesman"));
    startActivity(redoIntent);
  }
}




Java Source Code List

com.vinit.orderplacer.CategoryList.java
com.vinit.orderplacer.CheckPin.java
com.vinit.orderplacer.CommentBox.java
com.vinit.orderplacer.CounterOnClickListener.java
com.vinit.orderplacer.DumpJSON.java
com.vinit.orderplacer.IPGetter.java
com.vinit.orderplacer.RetrieveJSON.java
com.vinit.orderplacer.RetrieveSalesmen.java
com.vinit.orderplacer.SalesApp.java
com.vinit.orderplacer.SalesHistory.java
com.vinit.orderplacer.Salesman.java
com.vinit.orderplacer.SecurityPIN.java
com.vinit.orderplacer.SummaryActivity.java