dimanche 27 septembre 2015

How to check if a string exists in a List

Here is my code:

public List<Contact> getAllContacts() {
    List<Contact> contactList = new ArrayList<Contact>();
    String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {
            Contact contact = new Contact();
            contact.setID(Integer.parseInt(cursor.getString(0)));
            contact.setName(cursor.getString(1));
            contactList.add(contact);
        } while (cursor.moveToNext());
    }
     return contactList;
}

What I want is to compare all my contacts to one string and if they don't match, then to add that contact to my DB. The issue here is if I add the below code part, is that it checks if a single string matches and if it doesn't it gets add. I can tell that the code is wrong but I can't fix it.

List<Contact> contacts = db.getAllContacts();       
for (Contact cn : contacts) {
    if (!(cn.getName().contains(substring))) {
        // Inserting Contacts
        db.addContact(new Contact(record.get("9"), getDateTime()));
    }
}  

Aucun commentaire:

Enregistrer un commentaire