我搜索并发现:如何检查一个值是否已存在于 Android 的数据库中。我遇到的问题是吐司从不显示,当输入的值与数据库中已有的值匹配时,数据仍然输入到数据库中。我看过关于使用 UNIQUE 确保数据不重复的帖子,但它不适用于我需要做的事情。我试过 if (entries.contains(checkDuplicates) == false) 和 if (!entries.equals(checkDuplicates)),也得到了相同的结果。我知道这是我想念或不理解的简单事情。有人会帮助我并指出正确的方向吗?
private void saveState() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
eventDate = sdf.format(calendar.getTime());
String date = eventDate;
String event = eventTitle.getText().toString();
String empID = fiveAndTwo.getText().toString();
/**
* CRUD Operations
* */
// Inserting entries to DB
// Reading all contacts
Log.d("Reading: ", "Reading all entries..");
List<EventEntries> entries = db.getAllEntries();
for (EventEntries ee : entries) {
String log = "Id: " + ee.getID() + " Date: " + ee.getDate() + " Event Name: " + ee.getEvent() + " Emp ID: " + ee.getempID();
// Writing entries to log
Log.d("Entry: ", log);
}
String checkDuplicates = date + event + empID;
if (!entries.contains(checkDuplicates)) {
Log.d("Insert: ", "Inserting ..");
db.addEntry(new EventEntries(date, event, empID));
String logEntry = "Date: " + date + " Event Name: " + event + " Emp ID: " + empID;
Log.d("Entered to DB:", logEntry);
} else {
Toast.makeText(this,"Employee has already been scanned for this event.", Toast.LENGTH_SHORT).show();
}
}
编辑:我让它工作。这是我所做的:
private void saveState() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
eventDate = sdf.format(calendar.getTime());
String date = eventDate;
String event = eventTitle.getText().toString();
String empID = fiveAndTwo.getText().toString();
/**
* CRUD Operations
* */
// Reading all entries
Log.d("Reading: ", "Reading all entries..");
List<EventEntries> entries = db.getAllEntries();
for (EventEntries ee : entries) {
String log = "Id: " + ee.getID() + " Date: " + ee.getDate() + " Event Name: " + ee.getEvent() + " Emp ID: " + ee.getempID();
// Writing entries to log
Log.d("Entry: ", log);
}
EventEntries checkDuplicates = new EventEntries(date, event, empID);
if (!entries.contains(checkDuplicates)) {
Toast.makeText(this,"Successfully entered.", Toast.LENGTH_SHORT).show();
// Inserting entries to DB
Log.d("Insert: ", "Inserting ..");
db.addEntry(new EventEntries(date, event, empID));
String logEntry = "Date: " + date + " Event Name: " + event + " Emp ID: " + empID;
Log.d("Entered to DB:", logEntry);
} else {
empAlert();
}
}
这是我添加到 EventEntries 类的内容:
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((_date == null) ? 0 : _date.hashCode());
result = prime * result + ((_empID == null) ? 0 : _empID.hashCode());
result = prime * result + ((_event == null) ? 0 : _event.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
EventEntries other = (EventEntries) obj;
if (_date == null) {
if (other._date != null)
return false;
} else if (!_date.equals(other._date))
return false;
if (_empID == null) {
if (other._empID != null)
return false;
} else if (!_empID.equals(other._empID))
return false;
if (_event == null) {
if (other._event != null)
return false;
} else if (!_event.equals(other._event))
return false;
return true;
}