我正在尝试使用添加按钮在回收站视图中添加序列号。 需要检查是否尝试在回收者视图中添加重复值。
添加按钮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");
}
}
});
答案 0 :(得分:0)
您可以执行以下操作:
SerialNumberPojo serialNumberPojo = new SerialNumberPojo(serialNumberField.getText().toString());
if (!serialNumberPojoList.contains(serialNumberPojo)) {
serialNumberPojoList.add(serialNumberPojo);
}
在SerialNumberPojo中,您需要实现自己的equals(),如:
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (other == null || getClass() != other.getClass()) return false;
SerialNumberPojo that = (SerialNumberPojo) other;
if (getId() != null && getId().equals(that.getId())) {
return true;
}
return false;
}
答案 1 :(得分:0)
您可以在onCreate
函数中初始化回收视图。喜欢:
@Override
public void onCreate(...) {
...
recyclerView = findViewById(R.id.serial_recycle);
serialNumberAdapter = new SerialNumberAdapter(serialNumberPojoList, view.getContext(), ScannedDetailsFragment.this);
mLayoutManager = new LinearLayoutManager(view.getContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(serialNumberAdapter);
...
}
,然后在您的OnClickListener
中检查是否有重复,写为@Szymon Chaber
serialNumberAddButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!serialNumberField.getText().toString().equals("")) {
SerialNumberPojo serialNumberPojo = new SerialNumberPojo(serialNumberField.getText().toString());
if (!serialNumberPojoList.contains(serialNumberPojo)) {
serialNumberPojoList.add(serialNumberPojo);
} else {
// your action if found duplicate value
}
serialNumberAdapter.updateData(serialNumberPojoList);
serialNumberAdapter.notifyDataSetChanged();
actualQuantity.setText(String.valueOf(serialNumberAdapter.getItemCount()));
} else {
messageDialog.showAlertDialogBox(getContext(), "Add or Scan Serial Number", "error");
}
}
});