0

I have searched and found: How to check if a value already exists in the database in Android. The problem I am having is that the toast never shows and the data is still entered in the database when the values that are being entered match what is already in the database. I have seen post about using UNIQUE to make sure data is not duplicated, but it will not work for what I am needing to do. I have tried if (entries.contains(checkDuplicates) == false) and if (!entries.equals(checkDuplicates)), and got the same results with those as well. I know it is something simple that I am missing or not understanding. Will someone help me out and point mes in the right direction please?

    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();

    }
}

Edit: I got it to work. Here is what I did:

    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();

    }
}

And here is what I added to the EventEntries class:

    @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;
}
Community
  • 1
  • 1
Jasonwilliams10
  • 292
  • 1
  • 4
  • 17

2 Answers2

3

entries.contains(checkDuplicates) always returns false because entries is a list of EventEntries object and checkDuplicates is a String. You should call entries.contains using an EventEntries object as the parameter.

One more thing:

// 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

In general, the second case is false. It can be true, if in the EventEntries class you override the equals method so that first.equals(firstDupe) would return true. Only then, the second example will return true.

Here's an example implementation:

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;
}

(Adjust as necessary.)

janos
  • 120,954
  • 29
  • 226
  • 236
  • I tried the comment below with no success. Do I need to do something different than that to get an EventsEntries object? – Jasonwilliams10 Feb 22 '13 at 21:20
  • Thank you for the information. I got it to work with your help and I found http://stackoverflow.com/questions/4404084/find-if-a-value-exists-in-arraylist that helped me as well. – Jasonwilliams10 Feb 23 '13 at 02:35
3

Replace:

String checkDuplicates = date + event + empID;

with:

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

Adamantium
  • 118
  • 7
  • I tried this and it is still not working right. It never recognizes that information is already there. Changing to this makes date, event, and empID in to an EventEntries object, correct? Please forgive my ignorance, I am still new to android, java, and object oriented programming all together. – Jasonwilliams10 Feb 22 '13 at 21:19
  • Thank you for your help. I used a combination of your answer and the one above to get it to work. – Jasonwilliams10 Feb 23 '13 at 02:39