我搜索过并发现:How to check if a value already exists in the database in Android。我遇到的问题是,当输入的值与数据库中已有的值匹配时,toast永远不显示并且数据仍然输入数据库。我看过有关使用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;
}
答案 0 :(得分:3)
entries.contains(checkDuplicates)
始终返回false,因为entries
是EventEntries
对象的列表,checkDuplicates
是String
。您应该使用entries.contains
对象作为参数调用EventEntries
。
还有一件事:
// assuming entries is not empty
EventEntries first = entries.get(0);
entries.contains(first); // --> this is TRUE
// create a dupe of "first"
EventEntries firstDupe = new EventEntries(.....);
entries.contains(firstDupe); // --> in *general* this is FALSE
一般来说,第二种情况是错误的。如果在EventEntries
类中覆盖equals
方法以使first.equals(firstDupe)
返回true,则可以如此。只有这样,第二个例子才会返回true。
以下是一个示例实现:
public boolean equals(Object obj) {
if (obj instanceof EventEntries) {
EventEntries other = (EventEntries)obj;
if (date.equals(other.date) && event.equals(other.event)
&& empID == other.empID) {
return true;
}
}
return false;
}
(必要时调整。)
答案 1 :(得分:3)
替换:
String checkDuplicates = date + event + empID;
使用:
EventEntries checkDuplicates = new EventEntries(date, event, empID);