使用arraylist检查editText,不要让用户输入相同的数据

时间:2016-07-21 06:16:44

标签: android listview

用户可以添加产品& qty来自EditText,在按下的ADD按钮上显示在ListView,现在我不希望用户再次输入相同的产品..来自DB的获取数据...需要帮助,找到代码如下!

addBtn.setOnClickListener(new View.OnClickListener() {

               public void onClick(View v) {
                    String nullProduct = product.getText().toString();
                     nullQuant = quantity.getText().toString();

                    if (nullProduct.matches("") || nullQuant.matches("")) {
                        Toast.makeText(InvoiceView.this, "Please choose Product and Quantity", Toast.LENGTH_LONG).show();
                        return;
                    } else {

                        quant = Integer.parseInt(quantity.getText().toString());      // Calculation of quantity + unit price
                        unitprice = Double.parseDouble(prounitprice);
                        result = quant * unitprice;
                        Result = String.valueOf(result);
                                                // Total value of sales order
                        Map<String, String> dataFinal = new HashMap<String, String>();
                        dataFinal.put("A", proname);
                        dataFinal.put("B", String.valueOf(quant));
                        dataFinal.put("C", String.valueOf(unitprice));
                        dataFinal.put("D", Result);
                        proFinal.add(dataFinal);

                        String[] from = {"A", "B", "C", "D"};
                        int[] listviews = {R.id.lblproname, R.id.lblproqty, R.id.lblprorate, R.id.lblprototal};
                        final SimpleAdapter ADA = new SimpleAdapter(InvoiceView.this,
                                proFinal, R.layout.lsttemplate, from,
                                listviews);
                        lstpro.setAdapter(ADA);

                        int count = proFinal.size();
                        for (int i = 0; i < count; i++) {

                            HashMap<String, String> map = (HashMap<String, String>) proFinal.get(i);
                            String sValue;
                            if (proFinal.size() > 1) {
                                sValue = map.get("A");
                                if (nullProduct.equals(sValue)) {
                                    Toast.makeText(InvoiceView.this, "Product already added", Toast.LENGTH_SHORT).show();
                                } else {
                             // incomplete code section

                                }
                            }
                        }
                        }                      
                        product.setText("");
                        quantity.setText("");

                }
            });

    }

1 个答案:

答案 0 :(得分:0)

将产品名称保存为哈希映射中的唯一键。将地图密钥与输入的产品进行比较,如果地图没有您输入的产品作为密钥,则保存它。否则显示消息,如“产品已存在.. !!”。

尝试以下代码:

  Map<String, String> dataFinal = new HashMap<String, String>();
    dataFinal.put("A", "name");
    dataFinal.put("B", "String.valueOf(quant)");
    dataFinal.put("C", "String.valueOf(unitprice)");
    dataFinal.put("D", "Result");

    for (int i = 0; i < dataFinal.size(); i++) {
        String yourEnteredProduct = "D";//Assume new product D which is already existed,so you should not save it
        if (!dataFinal.containsKey(yourEnteredProduct)) {
            //Save you newly entered product
        } else {
            //Product already exists
        }
    }

希望它会对你有所帮助。