0

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.

3
  • can you post where you fill emailsList and registeredContactsList Commented May 10, 2015 at 13:59
  • 1
    @ElJazouli okay i'll update my post. Commented May 10, 2015 at 14:00
  • I'm sensing registeredContactsList is not filled correctly, can you do a little test and log for us the content of both lists ? Commented May 10, 2015 at 14:03

2 Answers 2

2

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");
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hey dude thanks for pointing out my error! And also for explaining this to me. I'll accept your answer in a bit. This helped me solved my problem :) Cheers!
0

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.

2 Comments

Yes I checked my list size and it gives the correct size. I also printed list's values
@bEtTyBarnes so, I think JBNizet has a good point, check his answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.