0

我搜索并发现:如何检查一个值是否已存在于 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;
}
4

2 回答 2

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。只有这样,第二个示例才会返回 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;
}

(根据需要进行调整。)

于 2013-02-22T19:03:20.787 回答
3

代替:

String checkDuplicates = date + event + empID;

和:

EventEntries checkDuplicates = new EventEntries(date, event, empID);

于 2013-02-22T19:18:12.010 回答