· 7 years ago · Sep 11, 2018, 05:52 AM
1Create and display a ListView of the ZXing ProductDatabase
2@Override
3 public void onCreate (Bundle savedInstanceState) {
4 super.onCreate(savedInstanceState);
5 setContentView(R.layout.spot_pay);
6 Button addButton = (Button) findViewById (R.id.addMenuButton);
7 addButton.setOnClickListener (new OnClickListener(){
8 public void onClick (View v){
9 startActivity(new Intent(CodiceBarreActivity.this, AggiungiCodiceActivity.class));
10 }
11 });
12 }
13static final class ProductData {
14 String barcode;
15 String format;
16 String title;
17 BigDecimal price;
18}
19
20private SQLiteDatabase db;
21
22 private static class ProductDatabaseHelper extends SQLiteOpenHelper {
23
24 public ProductDatabaseHelper(Context context) {
25 super(context, DATABASE_NAME, null, DATABASE_VERSION);
26 }
27
28 @Override
29 public void onCreate(SQLiteDatabase db) {
30 StringBuilder sql = new StringBuilder();
31
32 sql.append("create table ").append(PRODUCT_TABLE)
33 .append("( ")
34 .append(" _id integer primary key,")
35 .append(" barcode text,")
36 .append(" format text,")
37 .append(" title text,")
38 .append(" price currency")
39 .append(") ");
40
41 db.execSQL(sql.toString());
42
43 Log.d(TAG, PRODUCT_TABLE + "table created");
44
45 }
46
47 @Override
48 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
49 db.execSQL("drop table if exists " + PRODUCT_TABLE);
50 Log.d(TAG, PRODUCT_TABLE + "table dropped");
51 onCreate(db);
52 }
53 }
54
55 public CodiciDatabase(Context context) {
56 ProductDatabaseHelper helper = new ProductDatabaseHelper(context);
57 db = helper.getWritableDatabase();
58 }
59
60 public boolean insert(ProductData product) {
61 ContentValues vals = new ContentValues();
62 vals.put("barcode", product.barcode);
63 vals.put("format", product.format);
64 vals.put("title", product.title);
65 vals.put("price", product.price.multiply(ONE_HUNDRED).longValue());
66
67 return db.insert(PRODUCT_TABLE, null, vals) != -1;
68 }
69
70private static final int REQUEST_BARCODE = 0;
71 private static final ProductData mProductData = new ProductData();
72
73
74
75
76
77 private EditText mBarcodeEdit;
78 private EditText mFormatEdit;
79 private EditText mTitleEdit;
80 private EditText mPriceEdit;
81 private Button mScanButton;
82 private Button mAddButton;
83 private CodiciDatabase mProductDb;
84
85 @Override
86 protected void onCreate(Bundle savedInstanceState) {
87 super.onCreate(savedInstanceState);
88 setContentView(R.layout.add_product);
89
90 mBarcodeEdit = (EditText) findViewById(R.id.barcodeEdit);
91 mFormatEdit = (EditText) findViewById(R.id.codeFormatEdit);
92 mTitleEdit = (EditText) findViewById(R.id.titleEdit);
93 mPriceEdit = (EditText) findViewById(R.id.priceEdit);
94 mScanButton = (Button) findViewById(R.id.scanButton);
95 mScanButton.setOnClickListener(this);
96 mAddButton = (Button) findViewById(R.id.addButton);
97 mAddButton.setOnClickListener(this);
98 mProductDb = new CodiciDatabase(this); // not yet shown
99 }
100
101
102
103
104 public void onClick(View v) {
105 switch (v.getId()) {
106 case R.id.scanButton:
107 Intent intent = new Intent ("com.google.zxing.client.android.SCAN");
108 intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
109 startActivityForResult(intent, REQUEST_BARCODE);
110 break;
111
112 case R.id.addButton:
113 String barcode = mBarcodeEdit.getText().toString();
114 String format = mFormatEdit.getText().toString();
115 String title = mTitleEdit.getText().toString();
116 String price = mPriceEdit.getText().toString();
117
118 String errors = validateFields(barcode, format, title, price);
119 if (errors.length() > 0) {
120 showInfoDialog(this, "Please fix errors", errors);
121 } else {
122 mProductData.barcode = barcode;
123 mProductData.format = format;
124 mProductData.title = title;
125 mProductData.price = new BigDecimal(price);
126
127 mProductDb.insert(mProductData);
128 showInfoDialog(this, "Success", "Product saved successfully");
129 resetForm();
130 }
131 break;
132 }
133 }
134
135 }
136
137
138 private void resetForm() {
139 mBarcodeEdit.getText().clear();
140 mFormatEdit.getText().clear();
141 mTitleEdit.getText().clear();
142 mPriceEdit.getText().clear();
143
144 }
145
146
147 private void showInfoDialog(Context context, String title, String information) {
148 new AlertDialog.Builder (context)
149 .setMessage(information)
150 .setTitle(title)
151 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
152
153
154 public void onClick(DialogInterface dialog, int which) {
155 dialog.dismiss();
156
157 }
158 }).show();
159 }
160
161
162 public void onActivityResult(int requestCode, int resultCode, Intent intent){
163 if (requestCode == REQUEST_BARCODE){
164 if (resultCode == RESULT_OK) {
165 String barcode = intent.getStringExtra("SCAN_RESULT");
166 mBarcodeEdit.setText(barcode);
167
168 String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
169 mFormatEdit.setText(format);
170 } else if (resultCode == RESULT_CANCELED){
171 finish();
172 }
173
174 }
175 }
176}
177
178 private static String validateFields(String barcode, String format,
179 String title, String price) {
180 StringBuilder errors = new StringBuilder();
181
182 if (barcode.matches("^\s*$")) {
183 errors.append("Barcode requiredn");
184 }
185
186 if (format.matches("^\s*$")) {
187 errors.append("Format requiredn");
188 }
189
190 if (title.matches("^\s*$")) {
191 errors.append("Title requiredn");
192 }
193
194 if (!price.matches("^-?\d+(.\d+)?$")) {
195 errors.append("Need numeric pricen");
196 }
197
198 return errors.toString();
199 }
200}