· 7 years ago · Feb 27, 2019, 12:06 AM
1public Cursor food_items = null;
2private String item_name;
3private String item_descrp;
4private String item_price;
5private ArrayList<ItemDetails> image_details;
6
7private String item_id;
8private Cursor order_item;
9private Button order_btn;
10private Button cancel_btn;
11private SQLiteDatabase db;
12
13private AlertDialog mydialog;
14protected String table_no;
15protected String order_no;
16protected int quantity;
17protected int itemid;
18protected Cursor temp_item;
19public float Total = 0;
20private ListView lv1;
21public int bill_count = 100;
22private Cursor fav_item;
23private String table_name;
24private Calendar c;
25private TextView Title_txt;
26private String device_id;
27private String print_order_items = "";
28private String col_name;
29
30public static String APP_ID = "346381592110362"; // Replace your App ID
31
32public Facebook facebook;
33public AsyncFacebookRunner mAsyncRunner;
34String FILENAME = "AndroidSSO_data";
35public SharedPreferences mPrefs;
36private AlertDialog noItemalert;
37private Dialog checkOut;
38private Cursor mac;
39private String Main_table = "T1";
40
41/** Called when the activity is first created. */
42@Override
43public void onCreate(Bundle savedInstanceState) {
44 super.onCreate(savedInstanceState);
45 setContentView(R.layout.layout_myorder_view);
46 db = openOrCreateDatabase("restaurant", MODE_WORLD_READABLE, null);
47 c = Calendar.getInstance();
48
49 // ----initilaizee facebook objects-
50 facebook = new Facebook(APP_ID);
51
52 mAsyncRunner = new AsyncFacebookRunner(facebook);
53 SessionStore.restore(facebook, this);
54
55 SessionEvents.addAuthListener(new SampleAuthListener());
56 SessionEvents.addLogoutListener(new SampleLogoutListener());
57 table_name = getIntent().getStringExtra("table_name");
58 // WifiManager wifiManager = (WifiManager)
59 // getSystemService(Context.WIFI_SERVICE);
60 // WifiInfo wInfo = wifiManager.getConnectionInfo();
61 // String macAddress = wInfo.getMacAddress();
62 //
63 // Log.d("IMEI no.", macAddress);
64 // mac = db.rawQuery("select * from imei_tbl where imei_code='"
65 // + macAddress + "'", null);
66 // mac.moveToFirst();
67 // if (mac != null)
68 // Main_table = mac.getString(1);
69
70 lv1 = (ListView) findViewById(R.id.myorder_lv);
71 Title_txt = (TextView) findViewById(R.id.myorder_total);
72 Title_txt.setText("Total :" + Total);
73
74 order_btn = (Button) findViewById(R.id.myorder_btn);
75 cancel_btn = (Button) findViewById(R.id.myorder_backbtn);
76
77 cancel_btn.setOnClickListener(new View.OnClickListener() {
78
79 @Override
80 public void onClick(View v) {
81 Intent intent = new Intent(MyOrderList.this,
82 FoodCategoryTabs.class);
83 Bundle extras = new Bundle();
84 extras.putString("category_id", "food_category");
85 extras.putString("category_name", "food_category_id");
86 intent.putExtras(extras);
87 startActivity(intent);
88
89 }
90 });
91
92 // ----------------on order click--------------
93 order_btn.setOnClickListener(new View.OnClickListener() {
94
95 @Override
96 public void onClick(View v) {
97 int flagx = 0;
98 if (food_items.getCount() != 0)
99 showpopup();
100 else
101 checkOutPopUp(flagx);
102
103 }
104
105 });
106
107}
108
109protected void showpopup() {
110 final int flag = 1;
111
112 mydialog = new AlertDialog.Builder(this)
113 .setTitle("My Order Alert")
114 .setMessage(
115 "Thank you Dear Customer, Your Order will processed soon Would you like to proceed further...")
116 .setPositiveButton("Ok", new AlertDialog.OnClickListener() {
117
118 public void onClick(DialogInterface dialog, int which) {
119
120 continueFunction();
121 checkOutPopUp(flag);
122 }
123 })
124 .setNeutralButton("Discard", new AlertDialog.OnClickListener() {
125 public void onClick(DialogInterface dialog, int which) {
126 mydialog.dismiss();
127
128 }
129 })
130
131 .create();
132 mydialog.show();
133}
134
135protected void checkOutPopUp(final int flag_value) {
136 checkOut = new AlertDialog.Builder(MyOrderList.this)
137 .setTitle("CheckOut Alert !")
138 .setMessage(
139 "Would you like to continue to add items or checkout..")
140 .setPositiveButton("Continue",
141 new AlertDialog.OnClickListener() {
142
143 public void onClick(DialogInterface dialog,
144 int which) {
145
146 Intent intent = new Intent(MyOrderList.this,
147 FoodCategoryTabs.class);
148 Bundle extras = new Bundle();
149 extras.putString("category_id", "food_category");
150 extras.putString("category_name",
151 "food_category_id");
152 intent.putExtras(extras);
153 startActivity(intent);
154
155 }
156
157 })
158 .setNegativeButton("Checkout",
159 new AlertDialog.OnClickListener() {
160 public void onClick(DialogInterface dialog,
161 int which) {
162 if (flag_value != 0) {
163 Toast.makeText(getApplicationContext(),
164 "checkout click", Toast.LENGTH_LONG)
165 .show();
166 DBConnection dbConnection = new DBConnection(
167
168 getApplicationContext());
169 try {
170
171 dbConnection.createSocket();
172 dbConnection.dataOutputStream
173 .writeUTF("3");
174 } catch (Exception e) {
175
176 }
177
178 db.delete(table_name, "order_id=100", null);
179
180 Cursor fav_cursor = db.rawQuery(
181 "select * from temp_favourite_tbl",
182 null);
183 fav_cursor.moveToFirst();
184 int length = fav_cursor.getCount();
185 try {
186 dbConnection.dataOutputStream
187 .write(length);
188 } catch (IOException e1) {
189 // TODO Auto-generated catch block
190 e1.printStackTrace();
191 }
192
193 if (fav_cursor != null) {
194 do {
195 try {
196 String query = "insert into favourite_tbl values('";
197 query = query
198 + fav_cursor
199 .getString(0)
200 + "','"
201 + fav_cursor
202 .getString(1)
203 + "')";
204 dbConnection.dataOutputStream
205 .writeUTF(query);
206 } catch (Exception e) {
207
208 }
209
210 } while (fav_cursor.moveToNext());
211 }
212 db.execSQL("DROP TABLE IF EXISTS temp_favourite_tbl");
213 db.execSQL("create table if not exists temp_favourite_tbl(item_id varchar2,user_id varchar2,primary key(item_id))");
214 }
215 if (HeaderFooterActivity.facebook != null) {
216 if (HeaderFooterActivity.facebook
217 .isSessionValid()) {
218 ContentValues cv = new ContentValues();
219 cv.put("login_txt", "Login");
220 cv.put("username", "");
221 cv.put("user_id", "");
222 db.update("login_tbl", cv, null, null);
223
224 HeaderFooterActivity.mAsyncRunner
225 .logout(getApplicationContext(),
226 new LogoutRequestListener());
227
228 }
229 } else {
230
231 Log.d("logout me exeption", "");
232 }
233
234 Intent intent = new Intent(MyOrderList.this,
235 GridViewMenu.class);
236
237 startActivity(intent);
238
239 }
240 })
241
242 .create();
243 checkOut.show();
244
245}
246
247@Override
248protected void onResume() {
249 // TODO Auto-generated method stub
250 super.onResume();
251 table_name = getIntent().getStringExtra("table_name");
252 getFoodItems(table_name);
253 // calculateOrder();
254 final ListView lv1 = (ListView) findViewById(R.id.myorder_lv);
255 MyOrderAdapter ad = new MyOrderAdapter(this, image_details, table_name);
256 ad.notifyDataSetChanged();
257 lv1.setAdapter(ad);
258}
259
260public void continueFunction() {
261 try {
262 Cursor customer_order = db.rawQuery(
263 "select bill_no from auto_bill", null);
264 customer_order.moveToFirst();
265
266 String bill_no = Main_table + customer_order.getCount();
267 DBConnection dbConnection = new DBConnection(
268 getApplicationContext());
269 dbConnection.createSocket();
270
271 dbConnection.dataOutputStream.writeUTF("2");
272
273 dbConnection.dataOutputStream
274 .writeUTF("insert into auto_bill(bill_no) values('"
275 + bill_no + "')");
276 db.execSQL("insert into auto_bill(bill_no) values('" + bill_no
277 + "')");
278
279 System.out.println("passed");
280
281 dbConnection.dataOutputStream
282 .writeUTF("insert into CUSTOMER_BILL(table_no,bill_no,status,price,day,month,year) values('"
283 + Main_table
284 + "','"
285 + bill_no
286 + "','UNPAID',"
287 + Total
288 + ",'"
289 + c.get(Calendar.DAY_OF_MONTH)
290 + "','"
291 + (c.get(Calendar.MONTH) + 1)
292 + "','"
293 + c.get(Calendar.YEAR) + "')");
294 Log.d("Month", "" + c.get(Calendar.MONTH));
295 ArrayList<String> specialReq = new ArrayList<String>();
296 ArrayList<String> quantityList = new ArrayList<String>();
297 ArrayList<String> itemNameList = new ArrayList<String>();
298 String tempItemId = null;
299 Cursor cursor_order = db
300 .rawQuery(
301 "select item.Item_name, order_tbl.special_request,order_tbl.quantity from item, order_tbl where order_tbl.item_id=item.item_code",
302 null);
303 cursor_order.moveToFirst();
304 if (cursor_order != null) {
305 do {
306
307 itemNameList.add(cursor_order.getString(0));
308 quantityList.add(cursor_order.getInt(2) + "");
309
310 specialReq.add(cursor_order.getString(1));
311 } while (cursor_order.moveToNext());
312 dbConnection.dataOutputStream.write(itemNameList.size());
313 for (int l = 0; l < itemNameList.size(); l++) {
314 dbConnection.dataOutputStream.writeUTF(Main_table);
315 dbConnection.dataOutputStream.writeUTF(itemNameList.get(l));
316 dbConnection.dataOutputStream.writeUTF(specialReq.get(l));
317 dbConnection.dataOutputStream.writeUTF(quantityList.get(l));
318 }
319 }
320
321 } catch (Exception e) {
322 // TODO Auto-generated catch block
323 e.printStackTrace();
324 Log.d("EDC", "" + e);
325 }
326
327 db.delete(table_name, col_name + " = '" + order_no + "'", null);
328
329}
330
331public void getFoodItems(String tableName) {
332
333 food_items = db.rawQuery("select * from " + tableName, null);
334 food_items.moveToFirst();
335 image_details = getFavResults();
336 if (food_items.getCount() != 0) {
337 Log.d("count khali nahi h", "bilkl nai h");
338
339 calculateOrder();
340
341 }
342
343}
344
345public void calculateOrder() {
346 Cursor order;
347
348 Cursor customer_order;
349 String bill_no;
350
351 int count;
352 Cursor favourite_cursor;
353
354 try {
355 DBConnection dbcon = new DBConnection(getApplicationContext());
356 db = dbcon.getConnection();
357 food_items.moveToFirst();
358
359 col_name = food_items.getColumnName(0);
360 Log.d("----------------col_name", col_name);
361
362 if (food_items != null) {
363 food_items.moveToFirst();
364 Log.d("----------------col_name", "" + food_items.getCount());
365 order_no = food_items.getString(0);
366
367 do {
368 order = db.rawQuery(
369 "select item_price,item_name from item where item_code='"
370 + food_items.getString(1) + "'", null);
371 order.moveToFirst();
372 quantity = food_items.getInt(3);
373 Total += quantity * order.getInt(0);
374 Title_txt.setText("Total :" + Total);
375 Log.d("-------to", "" + Total);
376 } while (food_items.moveToNext());
377 }
378
379 } catch (Exception e) {
380 Log.d("dialog error", e.toString());
381 }
382
383}
384
385private ArrayList<ItemDetails> getFavResults() {
386 ArrayList<ItemDetails> results = new ArrayList<ItemDetails>();
387
388 try {
389 if (food_items != null) {
390 Log.d("order_tbl ID", "" + food_items.getString(1));
391 do {
392 fav_item = db.rawQuery(
393 "select * from item where item_code='"
394 + food_items.getString(1) + "'", null);
395 fav_item.moveToFirst();
396 item_id = fav_item.getString(1);
397 item_name = fav_item.getString(0);
398 item_descrp = fav_item.getString(2);
399 item_price = fav_item.getString(17);
400 ItemDetails item_details = new ItemDetails();
401 item_details.setName(item_name);
402 item_details.setItem_id(item_id);
403 item_details.setItemDescription(item_descrp);
404 item_details.setPrice(item_price);
405 item_details.setImage(fav_item.getBlob(10));
406 item_details.setcount(food_items.getInt(3));
407 results.add(item_details);
408 } while (food_items.moveToNext());
409
410 }
411 } catch (Exception e) {
412 Log.d("Coooooool Errorrrrrrrrrrrr", e.toString());
413 }
414 db.close();
415 return results;
416}
417
418@Override
419public void notifyDataSetChanged() {
420 // TODO Auto-generated method stub
421 super.notifyDataSetChanged();
422}
423
424private static ArrayList<ItemDetails> itemDetailsrrayList;
425
426private Integer[] imgid = { R.drawable.coffee, R.drawable.coffee,
427 R.drawable.coffee, R.drawable.coffee, R.drawable.coffee,
428 R.drawable.coffee };
429
430private LayoutInflater l_Inflater;
431
432private Context context;
433
434private String table_name;
435
436public MyOrderAdapter(Context context, ArrayList<ItemDetails> results,
437 String table_name) {
438 itemDetailsrrayList = results;
439 l_Inflater = LayoutInflater.from(context);
440 this.context = context;
441 this.table_name = table_name;
442
443}
444
445
446public int getCount() {
447 return itemDetailsrrayList.size();
448}
449
450public Object getItem(int position) {
451 return itemDetailsrrayList.get(position);
452}
453
454public long getItemId(int position) {
455 return position;
456}
457
458public View getView(int position, View convertView, ViewGroup parent) {
459 ViewHolder holder = new ViewHolder();
460 if (convertView == null) {
461 convertView = l_Inflater.inflate(R.layout.layout_myorder, parent,
462 false);
463
464 holder.txt_itemName = (TextView) convertView
465 .findViewById(R.id.myorder_titletag);
466
467 holder.txt_itemPrice = (TextView) convertView
468 .findViewById(R.id.myorder_price);
469 holder.txt_itemQuantity = (TextView) convertView
470 .findViewById(R.id.myorder_quantity);
471 holder.itemImage = (ImageView) convertView
472 .findViewById(R.id.myorder_food_img);
473 holder.plusBtn = (ImageButton) convertView
474 .findViewById(R.id.myorder_plus_btn);
475 holder.minusBtn = (ImageButton) convertView
476 .findViewById(R.id.myorder_minus_btn);
477 holder.removeBtn = (Button) convertView
478 .findViewById(R.id.myorder_remove_btn);
479 holder.edittext = (TextView) convertView
480 .findViewById(R.id.myorder_edit_txt);
481
482 holder.plusBtn.setOnClickListener(new View.OnClickListener() {
483
484 private SQLiteDatabase db;
485 private ItemDetails item_list;
486 private ContentValues cv;
487 int count = 1;
488
489 @Override
490 public void onClick(View v) {
491 item_list = itemDetailsrrayList.get((Integer) v.getTag());
492 count = item_list.getcount();
493 count++;
494
495 try {
496 DBConnection dbcon = new DBConnection(context);
497 db = dbcon.getConnection();
498 cv = new ContentValues();
499 cv.put("quantity", count);
500 db.update(table_name, cv,
501 "item_id='" + item_list.getItem_id() + "'",
502 null);
503 Intent intent = new Intent(context
504 .getApplicationContext(), MyOrderList.class);
505 intent.putExtra("table_name", table_name);
506 context.startActivity(intent);
507 } catch (Exception e) {
508 Log.d("Count", "" + e);
509 }
510
511 }
512 });
513
514 holder.minusBtn.setOnClickListener(new View.OnClickListener() {
515
516 private SQLiteDatabase db;
517 private ItemDetails item_list;
518 private ContentValues cv;
519 int count;
520
521 @Override
522 public void onClick(View v) {
523
524 item_list = itemDetailsrrayList.get((Integer) v.getTag());
525 count = item_list.getcount();
526
527 if (count > 1)
528 count--;
529 else
530 count = 1;
531
532 try {
533 DBConnection dbcon = new DBConnection(context);
534 db = dbcon.getConnection();
535 cv = new ContentValues();
536 cv.put("quantity", count);
537 db.update(table_name, cv,
538 "item_id='" + item_list.getItem_id() + "'",
539 null);
540 Intent intent = new Intent(context
541 .getApplicationContext(), MyOrderList.class);
542 intent.putExtra("table_name", table_name);
543 context.startActivity(intent);
544
545 } catch (Exception e) {
546 Log.d("Count", "" + e);
547 }
548
549 }
550 });
551
552 holder.removeBtn.setOnClickListener(new View.OnClickListener() {
553
554 private SQLiteDatabase db;
555
556 private ItemDetails item_list;
557 private ContentValues cv;
558
559 @Override
560 public void onClick(View v) {
561
562 try {
563 item_list = itemDetailsrrayList.get((Integer) v
564 .getTag());
565
566 DBConnection dbcon = new DBConnection(context);
567 db = dbcon.getConnection();
568
569 db.delete(table_name,
570 "item_id='" + item_list.getItem_id() + "'",
571 null);
572 db.close();
573
574 Intent intent = new Intent(context
575 .getApplicationContext(), MyOrderList.class);
576 intent.putExtra("table_name", table_name);
577 context.startActivity(intent);
578
579 } catch (Exception e) {
580 Log.d("errorrrrrr", e.toString());
581 }
582
583 }
584 });
585
586 convertView.setTag(holder);
587 } else {
588 holder = (ViewHolder) convertView.getTag();
589 }
590
591 holder.txt_itemName
592 .setText(itemDetailsrrayList.get(position).getName());
593 holder.txt_itemQuantity.setText("Quantity");
594
595 holder.txt_itemPrice.setText("Rs :"
596 + itemDetailsrrayList.get(position).getPrice());
597 holder.itemImage.setImageBitmap(itemDetailsrrayList.get(position)
598 .getImage());
599
600 holder.edittext.setText(Integer.toString(itemDetailsrrayList.get(
601 position).getcount()));
602
603 holder.plusBtn.setTag(position);
604 holder.minusBtn.setTag(position);
605
606 holder.removeBtn.setText("Remove");
607 holder.removeBtn.setTag(position);
608
609 return convertView;
610}
611
612public class ViewHolder {
613
614 public TextView edittext;
615 public TextView txt_itemQuantity;
616 public ImageButton minusBtn;
617 public ImageButton plusBtn;
618 public Button removeBtn;
619
620 TextView txt_itemName;
621 TextView txt_itemDescription;
622 TextView txt_itemPrice;
623 ImageView itemImage;
624
625 public ViewHolder() {
626 // TODO Auto-generated constructor stub
627 }
628
629}
630
631ArrayList<String> ids = new ArrayList<String>();
632ids.add("one");
633ids.add("two");
634ids.add("three");
635
636ListView lview = (ListView) findViewById(R.id.mylist);
637CustomAdapter adapter = new CustomAdapter(ids);
638lview.setAdapter(adapter);
639
640ids.clear();
641 ids.add("four");
642 ids.add("five");
643 ids.add("six");
644
645 adapter.notifySetDataChanged();