user1410081
user1410081

Reputation:

Android check if string in arraylist is equal

Hi guys I have a problem with my list. I need to check if a string is equal to one of the strings in my arraylist. So far it's working if there is only one equal string that matches in the list. However, if there are two or more strings, only one is being matched.

This is what I've tried so far:

viewHolder.tv.setText(namesList.get(position));
viewHolder.tvEmailAddress.setText(emailsList.get(position));

if (ConnectionDetector.hasNetworkConnection(getActivity())) {
    if(registeredContactsList != null) {
        for(String email : registeredContactsList) {
           if( emailsList.get(position).equals(email) ) {
               Log.d(TAG, "Registered: " + email);
               viewHolder.tvRegistered.setText("Add to Friends");
           } else {
               viewHolder.tvRegistered.setText("Invite");
           }
        }
    } else {
        viewHolder.tvRegistered.setVisibility(View.GONE);
    }
} else {
    viewHolder.tvRegistered.setVisibility(View.GONE);
}

UPDATE:

final int emailIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
String contactId, displayName, emailAddress;
while (cursor.moveToNext()) {
        contactId = cursor.getString(contactIdIndex);
        displayName = cursor.getString(displayNameIndex);
        emailAddress = cursor.getString(emailIndex);
        idsList.add(contactId);
        namesList.add(displayName);
        emailsList.add(emailAddress);
}

I should get two

  Add to Friends

in my listview. Assuming I have two strings matched in the arraylist.

Any ideas why I am getting only one string matched? Any help will be appreciated. Thanks.

Upvotes: 0

Views: 313

Answers (2)

JB Nizet
JB Nizet

Reputation: 691635

Suppose the email you're looking for is "[email protected]".

Suppose the list contains "[email protected]", "[email protected]", and "[email protected]".

What does your algorithm do? It loops through all elements, and at each iteration it sets the text of tvRegistered:

  • first iteration: alice is not equal to john, so the text is set to "Invite"
  • second iteration: john is equal to john, so the text is set to "Add to Friends"
  • third iteration: jack is not equal to john, so the text is set to "Invite".

And then the loop stops.

There is no reason to set the text multiple times. Your method is too long, and doesn't use the proper methods in the list.

You just need

if (registeredContactsList != null) {
    if (registeredContactsList.contains(email)) {
        Log.d(TAG, "Registered: " + email);
        viewHolder.tvRegistered.setText("Add to Friends");
    } 
    else {
        viewHolder.tvRegistered.setText("Invite");
    }
}

Upvotes: 2

Muhammed Refaat
Muhammed Refaat

Reputation: 9103

I think the problem is in your registeredContactsList, as when more than one email added to the list the new one overwrites the old one which makes you get only one email stored in the list even if it is repeated like million times, check the list size to make sure.

Upvotes: 0

Related Questions