Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ims.rms;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.graphics.Color;
- import android.os.AsyncTask;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.View.OnLongClickListener;
- import android.view.ViewGroup;
- import android.view.WindowManager;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.ArrayAdapter;
- import android.widget.BaseAdapter;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ImageButton;
- import android.widget.LinearLayout;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.Toast;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.util.ArrayList;
- import java.util.List;
- import de.keyboardsurfer.android.widget.crouton.Crouton;
- import de.keyboardsurfer.android.widget.crouton.Style;
- /***
- * <p/>
- * ListViewCustomAdapter is the custom adapter class to list the order item in
- * list view. It contain Item description with quantity.
- * <p/>
- */
- public class ListViewCustomAdapter extends BaseAdapter {
- private Context context;
- static ArrayList<String> listItem = new ArrayList<String>();
- static ArrayList<String> listBaseUnit = new ArrayList<String>();
- static ArrayList<Integer> listCount = new ArrayList<Integer>();
- static ArrayList<String> listMcode = new ArrayList<String>();
- static ArrayList<String> listKitchenDispatch = new ArrayList<String>();
- static ArrayList<String> listKOTTIME = new ArrayList<String>();
- static ArrayList<String> listAddedRemarks = new ArrayList<String>();
- static ArrayList<String> listIsBarItem = new ArrayList<String>();
- static ArrayList<String> listSno = new ArrayList<String>();
- static ArrayList<String> listKot = new ArrayList<String>(); // edited by Direndra Subba
- static ArrayList<Double> listRate = new ArrayList<Double>(); // edited by Direndra Subba
- JSONArray jsonArray = new JSONArray();
- String mcode, ret;
- String[] tmp;
- int pos;
- int mcase;
- View rowView;
- TextView tvCount, tvDescA, tvBaseUnit;
- String[] altUnit, confactor, rate;
- String username, password;
- ListView lvList;
- List<String> listRemarks;
- Integer isUnknown = 0;
- EditText etDESC, etRate;
- public ListViewCustomAdapter(Context context, JSONArray newJArray) { // edited by Direndra Subba
- this.context = context;
- jsonArray = newJArray;
- Log.i("listViewjsonArray", jsonArray.toString());
- listItem.clear();
- listBaseUnit.clear();
- listCount.clear();
- listMcode.clear();
- listKitchenDispatch.clear();
- listKOTTIME.clear();
- listAddedRemarks.clear();
- listIsBarItem.clear();
- listSno.clear();
- listKot.clear();
- listRate.clear();
- for (int i = 0; i < jsonArray.length(); i++) {
- try {
- JSONObject newJObject = jsonArray.getJSONObject(i);
- listItem.add(newJObject.getString("DESCA"));
- listBaseUnit.add(newJObject.getString("BASEUNIT"));
- listCount.add(newJObject.getInt("QUANTITY"));
- listMcode.add(newJObject.getString("MCODE"));
- listKitchenDispatch.add(newJObject.getString("KITCHENDISPATCH"));
- listKOTTIME.add(newJObject.getString("KOTTIME"));
- listAddedRemarks.add(newJObject.getString("REMARKS"));
- listIsBarItem.add(newJObject.getString("IsBarItem"));
- listSno.add(newJObject.getString("SNO"));
- listKot.add(newJObject.getString("KOT"));
- listRate.add(newJObject.getDouble("RATE"));
- } catch (JSONException e) {
- e.printStackTrace();
- } catch (NullPointerException e) {
- e.printStackTrace();
- }
- }
- } // edited by Direndra Subba
- @Override
- public View getView(final int position, View convertView, ViewGroup parent) {
- LayoutInflater inflater = (LayoutInflater) context
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- rowView = inflater.inflate(R.layout.listlayout, parent, false);
- tvDescA = (TextView) rowView.findViewById(R.id.tvDescA);
- tvCount = (TextView) rowView.findViewById(R.id.tvQty);
- tvBaseUnit = (TextView) rowView.findViewById(R.id.tvBunit);
- tvDescA.setText(listItem.get(position));
- tvDescA.setSelected(true);
- tvBaseUnit.setText(listBaseUnit.get(position));
- tvCount.setText("" + listCount.get(position));
- rowView.setOnLongClickListener(new OnLongClickListener() {
- @Override
- public boolean onLongClick(View arg0) {
- // TODO Auto-generated method stub
- pos = position;
- try { // edited by Diren
- if (GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(pos).getInt("QUANTITY") <= 0)
- return false;
- } catch (JSONException e) {
- e.printStackTrace();
- }
- try {
- mcode = GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(pos).getString("MCODE");
- } catch (JSONException e) {
- e.printStackTrace();
- }
- mcase = 0;
- new AsyncCall().execute();
- return false;
- }
- });
- return rowView;
- }
- @Override
- public int getCount() {
- int x = 0;
- try {
- x = listItem.size();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return x;
- }
- @Override
- public Object getItem(int position) {
- return null;
- }
- @Override
- public long getItemId(int position) {
- return 0;
- }
- class AsyncCall extends AsyncTask<String, Void, Void> {
- @Override
- protected Void doInBackground(String... params) {
- switch (mcase) {
- case 0:
- ret = "1";
- if (listKitchenDispatch.get(pos).equals("0")) {
- ret = WebService.invoke(MainActivity.tablenumber + "<;>" + listMcode.get(pos) + "<;>" + listKOTTIME.get(pos), "command",
- "getKitchenDispatch");
- }
- break;
- case 1:
- ret = WebService.invoke(mcode, "mcode", "getMultiAltUnit");
- break;
- case 2:
- ret = WebService.invoke3(username, password, "checkAccess");
- break;
- }
- return null;
- }
- @Override
- protected void onPostExecute(Void result) {
- // TODO Auto-generated method stub
- super.onPostExecute(result);
- switch (mcase) {
- case 0:
- try {
- isUnknown = jsonArray.getJSONObject(pos).getInt("IsUnknown");
- } catch (JSONException e) {
- e.printStackTrace();
- }
- if (getTotQuantity(mcode) == 0) {
- Crouton.makeText((Activity) context, "Total Remaining Qty : 0", Style.ALERT).show();
- } else {
- final Dialog dialog = new Dialog(context);
- dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
- dialog.setContentView(R.layout.listclickview);
- dialog.setCancelable(false);
- dialog.setTitle("Edit Order");
- etDESC = (EditText) dialog.findViewById(R.id.etDESC);
- etRate = (EditText) dialog.findViewById(R.id.etRate);
- TextView tvBASEUNIT = (TextView) dialog.findViewById(R.id.tvBASEUNT);
- final Button btnDecrease = (Button) dialog.findViewById(R.id.btnDecrease);
- etDESC.setEnabled(false);
- etDESC.setFocusableInTouchMode(false);
- etDESC.setFocusable(false);
- etDESC.setClickable(false);
- etDESC.setTextColor(Color.DKGRAY);
- etRate.setEnabled(false);
- etRate.setFocusableInTouchMode(false);
- etRate.setFocusable(false);
- etRate.setClickable(false);
- etRate.setTextColor(Color.DKGRAY);
- if (isUnknown == 1) {
- etDESC.setEnabled(true);
- etDESC.setFocusableInTouchMode(true);
- etDESC.setFocusable(true);
- etDESC.setClickable(true);
- etRate.setEnabled(true);
- etRate.setFocusableInTouchMode(true);
- etRate.setFocusable(true);
- etRate.setClickable(true);
- }
- final Button btnAltUnit = (Button) dialog.findViewById(R.id.btnAltUnit);
- btnAltUnit.setVisibility(View.GONE);
- final Button btnOk = (Button) dialog.findViewById(R.id.btnOk);
- Button btnCancel = (Button) dialog.findViewById(R.id.btnCancel);
- final TextView tvQUANTITY = (TextView) dialog.findViewById(R.id.tvQUANTITY);
- final EditText etRemarks = (EditText) dialog.findViewById(R.id.etRemarks);
- ImageButton imageBtnRemarks = (ImageButton) dialog.findViewById(R.id.btnSelectRemarks);
- etDESC.setText(listItem.get(pos));
- try {
- JSONObject jObj = jsonArray.getJSONObject(pos);
- etRate.setText(String.valueOf(jObj.getDouble("RATE")));
- } catch (JSONException e) {
- e.printStackTrace();
- }
- tvBASEUNIT.setText(listBaseUnit.get(pos));
- tvQUANTITY.setText("Total Qty : " + getTotQuantity(mcode));
- if (getTotQuantity(mcode) <= 1) {
- btnDecrease.setVisibility(View.GONE);
- }
- listKitchenDispatch.set(pos, ret);
- if (ret.equals("1")) {
- btnCancel.setVisibility(View.GONE);
- btnDecrease.setVisibility(View.GONE);
- btnAltUnit.setVisibility(View.GONE);
- etRemarks.setVisibility(View.GONE);
- }
- btnAltUnit.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- mcase = 1;
- new AsyncCall().execute();
- dialog.dismiss();
- }
- });
- btnDecrease.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- if (getTotQuantity(mcode) <= 1) {
- btnDecrease.setVisibility(View.GONE);
- return;
- }
- int index = getIndex();// edited by Direndra Subba
- try {
- if (index >= 0 && GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(index).getString("KOTTIME").equals("not set")) { // edited by Direndra Subba
- JSONObject jsonObject = GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(index);
- jsonObject.put("QUANTITY", listCount.get(index) - 1); //edited by Direndra Subba
- } else {
- addCancelOrder(pos, -1);
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- tvQUANTITY.setText("Total Qty : " + getTotQuantity(mcode));
- // }
- setListViewAdapter();
- }
- });
- btnCancel.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- try { //edited by Direndra Subba
- if (GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(pos).getString("KOTTIME").equals("not set")) {
- cancelOrder();
- } else {
- checkCancelAccess();
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- dialog.dismiss();
- }
- });
- imageBtnRemarks.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- ImsRmsDao db = new ImsRmsDao(context);
- final Dialog listDialog = new Dialog(context);
- listDialog.setContentView(R.layout.dialoglist);
- listDialog.setTitle("Remarks");
- listRemarks = db.listRemarks();
- lvList = (ListView) listDialog.findViewById(R.id.lvList);
- ArrayAdapter<String> dialogAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, listRemarks);
- lvList.setAdapter(dialogAdapter);
- lvList.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent,
- View view, int pos, long arg3) {
- // TODO Auto-generated method stub
- etRemarks.setText(parent.getItemAtPosition(pos).toString());
- listDialog.dismiss();
- }
- });
- listDialog.show();
- }
- });
- etRemarks.setOnLongClickListener(new OnLongClickListener() {
- @Override
- public boolean onLongClick(View v) {
- // TODO Auto-generated method stub
- ImsRmsDao db = new ImsRmsDao(context);
- final Dialog listDialog = new Dialog(context);
- listDialog.setContentView(R.layout.dialoglist);
- listDialog.setTitle("Remarks");
- listRemarks = db.listRemarks();
- lvList = (ListView) listDialog.findViewById(R.id.lvList);
- ArrayAdapter<String> dialogAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, listRemarks);
- lvList.setAdapter(dialogAdapter);
- lvList.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent,
- View view, int pos, long arg3) {
- // TODO Auto-generated method stub
- etRemarks.setText(parent.getItemAtPosition(pos).toString());
- listDialog.dismiss();
- }
- });
- listDialog.show();
- return false;
- }
- });
- btnOk.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Log.e("pos", pos + "");
- try {
- JSONObject jsonObject = GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(pos);
- if (!etRemarks.getText().toString().equals("")) {
- jsonObject.put("REMARKS", etRemarks.getText().toString()); //edited by Direndra Subba
- }
- if (jsonObject.getInt("IsUnknown") == 1) {
- jsonObject.put("DESCA", etDESC.getText().toString());
- jsonObject.put("RATE", etRate.getText().toString());
- MainActivity.lv.setAdapter(new ListViewCustomAdapter(context, GridCustomAdapter.listItemsObj.jsonArrayList));
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- for (int i = 0; i < GridCustomAdapter.listItemsObj.jsonArrayList.length(); i++) { // edited by Direndra Subba
- try {
- if (GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(i).getInt("QUANTITY") == 0)
- removeOrder(i);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- setListViewAdapter();
- dialog.dismiss();
- }
- });
- dialog.show();
- }
- break;
- case 1:
- if (ret.equals("no")) {
- Toast.makeText(context, "Error in loading unit", Toast.LENGTH_SHORT).show();
- } else {
- Log.e("unit", ret);
- tmp = ret.split("<;>");
- if (tmp.length > 0) {
- try {
- altUnit = tmp[0].split("<>");
- confactor = tmp[1].split("<>");
- rate = tmp[2].split("<>");
- loadAltUnit();
- } catch (Exception e) {
- // TODO: handle exception
- Toast.makeText(context, "Unit can't be changed", Toast.LENGTH_SHORT).show();
- }
- } else {
- Toast.makeText(context, "No alternate unit available", Toast.LENGTH_SHORT).show();
- }
- }
- break;
- case 2:
- if (ret.equals("1")) {
- cancelOrder();
- } else {
- Toast.makeText(context, "You do not have access to cancel order.", Toast.LENGTH_SHORT).show();
- }
- break;
- default:
- break;
- }
- }
- }
- public void setListViewAdapter() {
- // String strValue = etDESC.getText().toString();
- //
- // if (isUnknown ==
- // 0) {
- MainActivity.lv.setAdapter(new ListViewCustomAdapter(context, GridCustomAdapter.listItemsObj.jsonArrayList));
- // }
- // else{
- // MainActivity.lv.setAdapter(new ListViewAdapter(context,strValue));
- // }
- }
- private int getTotQuantity(String mcode) {
- int oldQuantity = 0, newQuantity = 0;
- for (int i = 0; i < GridCustomAdapter.listItemsObj.jsonArrayList.length(); i++) {
- try {
- JSONObject jsonObject = GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(i);
- if (jsonObject.getString("MCODE").equals(mcode)) {
- if (jsonObject.getString("KOTTIME").equals("not set"))
- newQuantity += jsonObject.getInt("QUANTITY");
- else
- oldQuantity += jsonObject.getInt("QUANTITY");
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- } // edited by Direndra Subba
- return oldQuantity + newQuantity;
- }
- private void removeOrder(int pos) {
- GridCustomAdapter.listItemsObj.removeOrderListItems(pos); // edited by Direndra Subba
- }
- private void addCancelOrder(int pos, int qty) {
- GridCustomAdapter.listItemsObj.addCancelOrderListItems(pos, qty); //edited by Direndra Subba
- }
- private void addChangeUnitOrder(int pos, String unit) {
- GridCustomAdapter.listItemsObj.addChangeUnitOrderListItems(pos, unit);
- }
- private int getCancellableQty(int pos) {
- int x = 0;
- try {
- if (getTotQuantity(mcode) > GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(pos).getInt("QUANTITY"))
- x = GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(pos).getInt("QUANTITY");
- else
- x = getTotQuantity(mcode);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- return x;
- }
- public void cancelOrder() {
- new AlertDialog.Builder(context)
- .setTitle("Cancel order?")
- .setMessage("Are you sure you want to cancel this order?")
- .setPositiveButton(android.R.string.yes,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog,
- int which) {
- try {
- if (GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(pos).getString("KOTTIME").equals("not set")) { // edited by Direndra Subba
- removeOrder(pos);
- } else {
- int index = getIndex(); // edited by Direndra Subba
- if (index > 0 && GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(index).getString("KOTTIME").equals("not set")) { // edited by Direndra Subba
- JSONObject jsonObject = GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(index);
- jsonObject.put("QUANTITY", listCount.get(index) - getCancellableQty(pos)); //edited by Direndra Subba
- } else {
- addCancelOrder(pos, 0 - getCancellableQty(pos));
- }
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- setListViewAdapter();
- }
- }
- )
- .setNegativeButton(android.R.string.no,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog,
- int which) {
- }
- }
- ).setIcon(android.R.drawable.btn_star).show();
- }
- public void checkCancelAccess() {
- final Dialog caDialog = new Dialog(context);
- caDialog.setContentView(R.layout.activity_user_login);
- caDialog.setTitle("Log In");
- final EditText etUserName = (EditText) caDialog.findViewById(R.id.etUserName);
- final EditText etPassword = (EditText) caDialog.findViewById(R.id.etPassword);
- LinearLayout llIp = (LinearLayout) caDialog.findViewById(R.id.llIp);
- llIp.setVisibility(View.GONE);
- Button btnLogin = (Button) caDialog.findViewById(R.id.btnLogin);
- btnLogin.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- username = etUserName.getText().toString();
- password = etPassword.getText().toString();
- if (username.equals("") || password.equals("")) {
- Toast.makeText(context, "Username or Password can't be empty.", Toast.LENGTH_SHORT).show();
- } else {
- mcase = 2;
- new AsyncCall().execute();
- }
- caDialog.dismiss();
- }
- });
- caDialog.show();
- }
- public void loadAltUnit() {
- final Dialog altDialog = new Dialog(context);
- altDialog.setContentView(R.layout.dialoglist);
- altDialog.setTitle("Alternate Unit");
- ListView lvList = (ListView) altDialog.findViewById(R.id.lvList);
- lvList.setAdapter(new NavListAdapter(context, altUnit, rate));
- lvList.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view,
- int position, long arg3) {
- // TODO Auto-generated method stub
- try {
- JSONObject jsonObject = GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(pos);
- if (jsonObject.getString("KOTTIME").equals("not set")) { // edited by Direndra Subba
- jsonObject.put("BASEUNIT", altUnit[position]); //edited by Direndra Subba
- } else {
- try {
- addCancelOrder(pos, 0 - jsonObject.getInt("QUANTITY"));// edited by Direndra Subba
- } catch (JSONException e) {
- e.printStackTrace();
- }
- addChangeUnitOrder(pos, altUnit[position]);
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- MainActivity.lv.setAdapter(new ListViewCustomAdapter(context, GridCustomAdapter.listItemsObj.jsonArrayList)); // edited by Direndra Subba
- altDialog.dismiss();
- }
- }
- );
- altDialog.show();
- }
- public int getIndex() { // edited by Direndra Subba
- int index = 0;
- breakLoop:
- for (int i = 0; i < jsonArray.length(); i++) {
- try {
- if (jsonArray.getJSONObject(i).getString("MCODE").equals(mcode)) {
- index = i;
- break breakLoop;
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- return index;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement