@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) { ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); if (matches.size() > 0) { String firstMatch = matches.get(0); String[] splitted = firstMatch.split("\\smais\\s|\\smas\\s"); List<ItensCompras> itens = new ArrayList<>(splitted.length); for (String nome : splitted) { ItensCompras item = new ItensCompras(); item.setListaCompras(listaCompras); String[] nomeSplit = nome.split(" "); if (nomeSplit.length > 1) { try { Double qtd = Double.parseDouble(nomeSplit[0]); item.setProduto(nomeSplit[1]); item.setQtde(qtd); } catch (NumberFormatException e) { item.setProduto(nome); item.setQtde(0d); } } item.setValorUnitario(0d); itens.add(item); } salvarItens(itens); } } super.onActivityResult(requestCode, resultCode, data); }
private void preencheCampos(ItensCompras item) { item.setProduto(((EditText) findViewById(R.id.nomeProduto)).getText().toString()); try { item.setQtde(Double.valueOf(((EditText) findViewById(R.id.qtde)).getText().toString())); } catch (NumberFormatException e) { item.setQtde(0d); } try { item.setValorUnitario( Double.valueOf(((EditText) findViewById(R.id.unitario)).getText().toString())); } catch (NumberFormatException e) { item.setValorUnitario(0d); } }