@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_item); client = new YardSaleApplication(this); String itemId = getIntent().getStringExtra("item_id"); ivItemPreview = (ImageView) findViewById(R.id.ivItemPreview); etEditItemDescription = (EditText) findViewById(R.id.etItemDescription); etEditItemPrice = (EditText) findViewById(R.id.etItemPrice); Button btnSaveItem = (Button) findViewById(R.id.btnSaveItem); ParseQuery getItemQuery = Item.getQuery(); try { item = (Item) getItemQuery.get(itemId); } catch (ParseException e) { e.printStackTrace(); } Picasso.with(this).load(item.getPhoto().getUrl()).into(ivItemPreview); etEditItemDescription.setText(item.getDescription()); etEditItemPrice.setText("$" + item.getPrice().toString()); etEditItemPrice.addTextChangedListener( new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void onTextChanged(CharSequence s, int start, int before, int count) { String current = ""; if (!s.toString().equals(current)) { etEditItemPrice.removeTextChangedListener(this); String cleanString = s.toString().replaceAll("[$,.]", ""); double parsed = Double.parseDouble(cleanString); String formatted = NumberFormat.getCurrencyInstance().format((parsed / 100)); current = formatted; etEditItemPrice.setText(formatted); etEditItemPrice.setSelection(formatted.length()); etEditItemPrice.addTextChangedListener(this); } } @Override public void afterTextChanged(Editable s) {} }); btnSaveItem.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { addItem(v); } }); }
public void addItem(View view) { Number price = Double.parseDouble(etEditItemPrice.getText().toString().replace("$", "")); String description = String.valueOf(etEditItemDescription.getText()); ParseFile imageParseFile; if (image != null) imageParseFile = new ParseFile(getBytesFromBitmap(image)); else imageParseFile = item.getParseFile("photo"); imageParseFile.saveInBackground(); client.updateItem( getSupportFragmentManager(), item.getObjectId(), description, price, imageParseFile, item.getYardSale()); Intent data = new Intent(); data.putExtra("price", String.valueOf(etEditItemPrice.getText())); data.putExtra("desc", String.valueOf(etEditItemDescription.getText())); data.putExtra("obj_id", item.getObjectId()); setResult(143, data); finish(); }