forked from homeinventpro/finalpro_github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicList_page.java
More file actions
461 lines (358 loc) · 19.1 KB
/
Copy pathBasicList_page.java
File metadata and controls
461 lines (358 loc) · 19.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package com.matan.listit;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.InputType;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import com.parse.FindCallback;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
public class BasicList_page extends ActionBarActivity implements View.OnClickListener, AdapterView.OnItemSelectedListener {
private int x=2;
private Button addBtn;
private TableLayout t1;
private TableRow tr;
private TextView proName, proCode;
String categorySelected, productSelected, proBarcode;
String[] categoryList = new String[] {"choose product category", "Cheese", "Deserts","Sauces"};
String[] ProductsList= new String [] {"choose product to add to your list", "X"};
String[] BarcodeList = new String[] {" "," "};
List<Integer> productsAmountList = new ArrayList<Integer>();
private EditText ammountText;
private CheckBox deleteCheckBox;
Spinner catSpinner,proSpinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_basic_list_page);
catSpinner =(Spinner)findViewById(R.id.categorySpinner);
proSpinner =(Spinner)findViewById(R.id.productSpinner);
addBtn=(Button)findViewById(R.id.Add_Button);
t1= (TableLayout)findViewById(R.id.ProdList);
t1.setColumnStretchable(0,true);
t1.setColumnStretchable(1,true);
catAdapter() ;
prodAdapter();
ParseQuery<ParseObject> query = ParseQuery.getQuery("MyList");
query.whereNotEqualTo("Code", " ");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> scoreList, ParseException e) {}
@Override
public void done(List<ParseObject> parseObjects, com.parse.ParseException e) {
for (int i = 0; i < parseObjects.size(); i++) {
ParseObject object = parseObjects.get(i);
productsAmountList.add(i,object.getInt("Ammount"));
TableLayout t = (TableLayout) findViewById(R.id.ProdList);
TableRow row = new TableRow(getApplicationContext());
TextView nameTextView = new TextView(getApplicationContext());
nameTextView.setText(object.getString("Product_Name"));
nameTextView.setTextColor(Color.BLACK);
TextView codeTextView = new TextView(getApplicationContext());
codeTextView.setText(object.getString("Code"));
codeTextView.setTextColor(Color.BLACK);
EditText amountEditText = new EditText(getApplicationContext());
amountEditText.setInputType(InputType.TYPE_CLASS_NUMBER);
amountEditText.setText(String.valueOf(productsAmountList.get(i)));
amountEditText.setTextColor(Color.BLACK);
CheckBox delCheckBox = new CheckBox(getApplicationContext());
delCheckBox.setTextColor(Color.BLACK);
row.addView(nameTextView);
row.addView(codeTextView);
row.addView(amountEditText);
row.addView(delCheckBox);
t.addView(row);
}
}
});
}
public void catAdapter(){
ArrayAdapter Catadapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_expandable_list_item_1, categoryList);
catSpinner.setAdapter(Catadapter);
catSpinner.setOnItemSelectedListener(this);
}
public void prodAdapter(){
ArrayAdapter prodadapter = new ArrayAdapter(getApplicationContext(),android.R.layout.simple_expandable_list_item_1, ProductsList);
proSpinner.setAdapter(prodadapter);
proSpinner.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView parent, View view, int pos,long id) {
categorySelected = catSpinner.getSelectedItem().toString();
final ParseQuery<ParseObject> query = ParseQuery.getQuery("PRODUCTS");
query.whereEqualTo("Category", categorySelected);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> prodList, java.text.ParseException e) {}
public void done(List<ParseObject> parseObjects, com.parse.ParseException e) {
if (e == null) {
for(int i=0; i<parseObjects.size(); i++) {
ProductsList[i] = parseObjects.get(i).get("Product_Name").toString();
BarcodeList[i] = parseObjects.get(i).get("Code").toString();
}
}
//prodAdapter();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_basic_list_page, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Add_Button:
Boolean flag = true;
productSelected = proSpinner.getSelectedItem().toString();
for (int i = 0; i < 2; i++) {
if (productSelected == ProductsList[i])
proBarcode = BarcodeList[i];
}
for (int i = 0; i < t1.getChildCount(); i++) {
TableRow checkIfRowExists = (TableRow) t1.getChildAt(i);
TextView productView = (TextView) checkIfRowExists.getVirtualChildAt(0);
TextView checkCode = (TextView) checkIfRowExists.getVirtualChildAt(1);
Toast.makeText(getApplicationContext(), "product " + proBarcode , Toast.LENGTH_LONG).show();
if (proBarcode == checkCode.getText() ) {
flag = false;
Toast.makeText(getApplicationContext(), "product " + productSelected + " is already listed ", Toast.LENGTH_LONG).show();
productView.setTextAppearance(getApplicationContext(), R.style.boldText);
productView.setBackgroundResource(R.color.bright_foreground_disabled_material_light);
break;
}
}
if (flag) {
tr = new TableRow(this);
proName = new TextView(this);
proCode = new TextView(this);
deleteCheckBox = new CheckBox(this);
ammountText = new EditText(this);
ammountText.setInputType(10);
proName.setText(productSelected);
proCode.setText(proBarcode);
ammountText.setText("1");
tr.addView(proName);
tr.addView(proCode);
tr.addView(ammountText);
tr.addView(deleteCheckBox);
t1.addView(tr);
break;
}
break;
case R.id.update_Button:
int ammountNum =1;
TextView proBoxToCheck = new TextView(this);
TextView codeBoxToCheck = new TextView(this);
EditText ammountToCheck = new EditText(this);
///////////////updating my list///////////////////////////////////////
for (int i=1, j=t1.getChildCount(); i<j; i++) {
TableRow rowToCheck = new TableRow(this);
rowToCheck= (TableRow) t1.getChildAt(i);
proBoxToCheck= (TextView) rowToCheck.getChildAt(0);
final String proToUpdate = proBoxToCheck.getText().toString();
codeBoxToCheck =(TextView) rowToCheck.getChildAt(1);
final String codeToUpdate = codeBoxToCheck.getText().toString();
ammountToCheck = (EditText) rowToCheck.getChildAt(2);
String ammountToUpdate =ammountToCheck.getText().toString();
try {
ammountNum = Integer.parseInt(ammountToUpdate);
}
catch (NumberFormatException nfe){}
ParseQuery<ParseObject> query = ParseQuery.getQuery("MyList");
query.whereEqualTo("Code",codeToUpdate);
final TableRow finalRowToCheck = rowToCheck;
final int finalAmmountNum = ammountNum;
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> products, ParseException e) { }
@Override
public void done(List<ParseObject> parseObjects, com.parse.ParseException e) {
ParseObject MyList = new ParseObject("MyList");
ParseObject Myinventory = new ParseObject("MyInventory");
ParseObject Cart = new ParseObject("Cart");
if(parseObjects.size()==0)
{
MyList.put("Product_Name",proToUpdate);
MyList.put("Code",codeToUpdate);
MyList.put("Ammount", finalAmmountNum);
MyList.saveInBackground();
Myinventory.put("Product_Name",proToUpdate);
Myinventory.put("Code",codeToUpdate);
Myinventory.put("In_Stock",0);
Myinventory.saveInBackground();
Cart.put("Product_Name",proToUpdate);
Cart.put("Code",codeToUpdate);
Cart.put("To_Buy",finalAmmountNum);
Cart.saveInBackground();
}
else
{
ParseObject X = parseObjects.get(0);
X.put("Ammount", finalAmmountNum);
X.saveInBackground();
}
}
});
}
////////////////updating in the cart/////////////////////////////////
for (int i=1, j=t1.getChildCount(); i<j; i++) {
TableRow rowToCheck = new TableRow(this);
rowToCheck= (TableRow) t1.getChildAt(i);
codeBoxToCheck =(TextView) rowToCheck.getChildAt(1);
final String codeToUpdate = codeBoxToCheck.getText().toString();
ammountToCheck = (EditText) rowToCheck.getChildAt(2);
String ammountToUpdate =ammountToCheck.getText().toString();
try {
ammountNum = Integer.parseInt(ammountToUpdate);
}
catch (NumberFormatException nfe){}
ParseQuery<ParseObject> query = ParseQuery.getQuery("Cart");
query.whereEqualTo("Code",codeToUpdate);
final TableRow finalRowToCheck = rowToCheck;
final int finalAmmountNum = ammountNum;
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> products, ParseException e) { }
@Override
public void done(List<ParseObject> parseObjects, com.parse.ParseException e) {
ParseObject MyList = new ParseObject("MyList");
ParseObject Myinventory = new ParseObject("MyInventory");
ParseObject Cart = new ParseObject("Cart");
if(parseObjects.size()==0)
{ }
else
{
ParseObject X = parseObjects.get(0);
X.put("To_Buy", finalAmmountNum);
X.saveInBackground();
}
}
});
}
break;
case R.id.delete_Button:
TableRow row = new TableRow(this);
TextView barcodeView = new TextView(this);
TextView nameView = new TextView(this);
CheckBox checkBox = new CheckBox(this);
String barcode, name;
for(int i=1 ;i <t1.getChildCount(); i++)
{
row= (TableRow) t1.getChildAt(i);
checkBox = (CheckBox) row.getVirtualChildAt(3);
if(checkBox.isChecked())
{
nameView = (TextView) row.getVirtualChildAt(0);
name = nameView.getText().toString();
barcodeView= (TextView) row.getVirtualChildAt(1);
barcode = barcodeView.getText().toString();
Toast.makeText(getApplicationContext(),name+ " deleted", Toast.LENGTH_LONG).show();
////////////////////delete from my list/////////////////////////////////////////////////////
ParseQuery<ParseObject> query = ParseQuery.getQuery("MyList");
query.whereEqualTo("Code", barcode);
final String finalBarcode = barcode;
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> scoreList, ParseException e) {}
@Override
public void done(List<ParseObject> parseObjects, com.parse.ParseException e) {
if (parseObjects.size()!=0) {
ParseObject toDelete = parseObjects.get(0);
toDelete.deleteInBackground();
}
}
});
}
}
for(int i=1 ;i <t1.getChildCount(); i++)
{
row= (TableRow) t1.getChildAt(i);
checkBox = (CheckBox) row.getVirtualChildAt(3);
if(checkBox.isChecked())
{
nameView = (TextView) row.getVirtualChildAt(0);
name = nameView.getText().toString();
barcodeView= (TextView) row.getVirtualChildAt(1);
barcode = barcodeView.getText().toString();
Toast.makeText(getApplicationContext(),name+ " deleted", Toast.LENGTH_LONG).show();
////////////////////delete from Cart/////////////////////////////////////////////////////
ParseQuery<ParseObject> query = ParseQuery.getQuery("Cart");
query.whereEqualTo("Code", barcode);
final String finalBarcode = barcode;
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> scoreList, ParseException e) {}
@Override
public void done(List<ParseObject> parseObjects, com.parse.ParseException e) {
if (parseObjects.size()!=0) {
ParseObject toDelete = parseObjects.get(0);
toDelete.deleteInBackground();
}
}
});
}
}
for(int i=1 ;i <t1.getChildCount(); i++)
{
row= (TableRow) t1.getChildAt(i);
checkBox = (CheckBox) row.getVirtualChildAt(3);
if(checkBox.isChecked())
{
t1.removeView(row);
nameView = (TextView) row.getVirtualChildAt(0);
name = nameView.getText().toString();
barcodeView= (TextView) row.getVirtualChildAt(1);
barcode = barcodeView.getText().toString();
////////////////////delete from my inventory/////////////////////////////////////////////////////
ParseQuery<ParseObject> query = ParseQuery.getQuery("MyInventory");
query.whereEqualTo("Code", barcode);
final String finalBarcode = barcode;
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> scoreList, ParseException e) {}
@Override
public void done(List<ParseObject> parseObjects, com.parse.ParseException e) {
if (parseObjects.size()!=0) {
ParseObject toDelete = parseObjects.get(0);
toDelete.deleteInBackground();
}
}
});
}
}
break;
//go iterative on the list and look for checkbox and delete this lines
}
}
/**
* Callback method to be invoked when the selection disappears from this
* view. The selection can disappear for instance when touch is activated
* or when the adapter becomes empty.
*
* @param parent The AdapterView that now contains no selected item.
*/
@Override
public void onNothingSelected(AdapterView<?> parent) {}
}