public void addToDatabase(String backendid) { String name = editName.getText().toString(); String amount = editAmount.getText().toString(); localDatabase database = new localDatabase(this); DateFormat format = new SimpleDateFormat("dd.MM.yyyy"); long expireDateMillis = 0; try { Date expireDate = format.parse(String.valueOf(dateEdit.getText())); expireDateMillis = expireDate.getTime(); } catch (ParseException e) { e.printStackTrace(); } JSONObject food = new JSONObject(); try { food.put("name", name); food.put("expire_date", expireDateMillis); food.put("backendId", Integer.parseInt(backendid)); food.put("category_id", database.getCategory(oCategorySpinner.getSelectedItem().toString())); food.put("position_id", database.getPosition(oPositionSpinner.getSelectedItem().toString())); food.put("amount", amount); food.put("unit", oUnitsSpinner.getSelectedItem().toString()); // food.put("entry_date", ) // add more values } catch (JSONException e) { e.printStackTrace(); } database.addFood(food); showNotification("Added " + name + " to database", ""); String dateString = dateEdit.getText().toString(); dateString = dateString.replaceAll("/", "."); Date dt = null; try { dt = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT, Locale.GERMANY) .parse(dateString); } catch (ParseException e) { e.printStackTrace(); } long insertedTimeInMillis = dt.getTime(); if (insertedTimeInMillis < new Date().getTime()) { showNotification("Expired!", name + " is expired on " + dt.toString()); } }
private void populateCategorySpinner() { JSONArray categories = database.getCategories(); ArrayList categoriesSpinnerFormat = new ArrayList(); JSONObject categoryElement; for (int i = 0; i < categories.length(); i++) { try { categoryElement = (JSONObject) categories.get(i); categoriesSpinnerFormat.add(categoryElement.get("name")); } catch (JSONException e) { e.printStackTrace(); } } categorySpinnerAdapter = new ArrayAdapter<String>( this, android.R.layout.simple_spinner_dropdown_item, categoriesSpinnerFormat); oCategorySpinner.setAdapter(categorySpinnerAdapter); }
private void populatePositionsSpinner() { JSONArray positions = database.getPositions(); ArrayList positionsSpinnerFormat = new ArrayList(); JSONObject positionElement; for (int i = 0; i < positions.length(); i++) { try { positionElement = (JSONObject) positions.get(i); positionsSpinnerFormat.add(positionElement.get("name")); } catch (JSONException e) { e.printStackTrace(); } } positionSpinnerAdapter = new ArrayAdapter<String>( this, android.R.layout.simple_spinner_dropdown_item, positionsSpinnerFormat); oPositionSpinner.setAdapter(positionSpinnerAdapter); }