【发布时间】:2018-11-16 11:14:53
【问题描述】:
我正在尝试使用添加按钮在回收站视图中添加序列号。 需要检查是否重复值试图添加到回收站视图中。
添加按钮 Onclick 监听代码如下
serialNumberAddButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!serialNumberField.getText().toString().equals("")) {
// here need to check the duplicate values
SerialNumberPojo serialNumberPojo = new SerialNumberPojo(serialNumberField.getText().toString());
serialNumberPojoList.add(serialNumberPojo);
RecyclerView recyclerView = view.findViewById(R.id.serial_recycle);
serialNumberAdapter = new SerialNumberAdapter(serialNumberPojoList, view.getContext(), ScannedDetailsFragment.this);
actualQuantity.setText(String.valueOf(serialNumberAdapter.getItemCount()));
mLayoutManager = new LinearLayoutManager(view.getContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(serialNumberAdapter);
serialNumberAdapter.notifyDataSetChanged();
} else {
messageDialog.showAlertDialogBox(getContext(), "Add or Scan Serial Number", "error");
}
}
});
【问题讨论】:
-
您可以通过
contains进行检查,也可以使用HashSet列表来防止重复值。 -
serialNumberPojoList 具有自定义数据类型 pojo 类,我尝试使用 contains 但不起作用
-
你需要在你的 pojo 中覆盖
equals()和hashcode()方法,之后contains才能工作。 -
public boolean equals(Object obj) { return super.equals(obj); } public int hashCode() { return super.hashCode();我已经在 pojo 类中实现了这个是好的还是需要这个方法上的代码
-
您需要在那里实现实际代码,例如。 G。 public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) 返回 false; SerialNumberPojo = (SerialNumberPojo) o; return !(getId() != null ? !getId().equals(that.getId()) : that.getId() != null); }
标签: android android-studio android-recyclerview