java - 比较字符串和对象属性
问题描述
我正在尝试使用 .equals() 将字符串与对象属性进行比较,但即使条件为真,条件也会失败。我尝试使用 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();
}
}
解决方案
推荐阅读
- php - PHP:用于 litespeed 的 x-sendfile
- unity3d - Unity 网络:使用同步列表字符串
- ios - 如何在 UIswitch 打开和关闭时上下移动标签?
- python-2.7 - 为什么这种错误的 python 格式会产生这个结果而不是异常?
- javascript - 键入时将字符更改为星号
- javascript - ReactJS 中的重组件在 IE 浏览器上执行缓慢
- bash - 替换多个文件中的多个字符串
- ruby-on-rails - 如何在没有 RubyInstaller 2.3.4 的情况下在 Windows 上安装 ruby 2.3.4?
- angular - 我如何从角度 5 中的其他组件更新组件数据
- neural-network - Yolo v3不能设置小于三个的bounding box的个数吗?