比较字符串和对象属性

时间:2019-03-09 16:28:00

标签: java android string object comparison

我正在尝试使用.equals()将字符串与对象属性进行比较,但条件始终失败,即使其为true。 我试过使用trim()摆脱尾随空格,但仍然失败。

这里是我的代码:

if (status) {
        inventoryList = allInventory.getInventory();
        assert assetId != null;
        for (Inventory inventory : inventoryList) {
            if (assetId.trim().equals(inventory.getProductCode().trim())) {

                //get the current quantity that exists

                new_quantity.setValue(0);

                new_quantity.setMaxValue(inventory.getQuantity());
                inventoryId = inventory.getId();
                return;

            } else {
                // new_quantity.setVisibility(View.GONE);
                decrement.setEnabled(false);
                AlertDialog.Builder alert = new AlertDialog.Builder(this);
                alert.setTitle("Product not found ");
                // prevent cancel of AlertDialog on click of back button and outside touch
                alert.setCancelable(false);
                alert.setMessage("Product is not in inventory. Do you want to continue scanning?");

                alert.setNegativeButton("No", (dialog, which) -> {
                            onBackPressed();
                            dialog.dismiss();
                        }
                );
                alert.setPositiveButton("Yes", (dialog, which) -> {
                    Intent scanAsset = new Intent(this, ScanActivity.class);
                    scanAsset.putExtra("building", building);
                    scanAsset.putExtra("room", room);
                    scanAsset.putExtra("department", depart);
                    scanAsset.putExtra("TAG", "E");
                    startActivity(scanAsset);
                    dialog.dismiss();
                });
                AlertDialog dialog = alert.create();
                dialog.show();
            }

        }

2 个答案:

答案 0 :(得分:1)

我想指出oracle文档,它说:

  

将此字符串与指定对象进行比较。 当且仅当参数不为null且为String对象(表示与此对象相同的字符序列)时,结果为true。

enter image description here

答案 1 :(得分:0)

尝试使用equalsIgnoreCase()代替equals