1

To study android development, I have created an android application called "Contacts", it's basically just a phonebook. I have a list of contacts with first name, last name and phone number. I also have an edittext which I use for filtering the list.

Currently, I can filter the list by finding all entries containing the string inputted in the edittext. For example, I have 3 contacts:

Jane Doe
John Doe
John Joseph Smith

If I input something like "oe", the list will return John Doe and Jane Doe. Now, I was hoping I could filtering by the initials of the name so if I enter "J S" or "Jo J S", it would return John Joseph Smith but I'm not entirely sure how to start this implementation.

I've overrided FilterResults performFiltering and this is what I ended up with:

FilterResults results = new FilterResults();
ArrayList<Contacts> filteredContacts= new ArrayList<Contacts>();

if (constraint!= null && constraint.toString().length() > 0) {
    for (Contacts contact : unfilteredContacts) {
        String firstname = contact.getFirstname();
        String lastname = contact.getLastname();
        String phone = contact.getNumber();

        if (firstname .toLowerCase().contains(constraint.toString().toLowerCase()) ||
                lastname .toLowerCase().contains(constraint.toString().toLowerCase()) ||
                phone .contains(constraint.toString())) {
            filteredContacts.add(contact);
        }
    }

    results.values = filteredContacts;
    results.count = filteredContacts.size();

What is the best approach in implementing this? Is there any way I can avoid using nested loops for this?

[UPDATE] I ended up using loops for my implementation however my code is acting a bit funny. I've added this snippet to perform the additional filter:

String[] splitName = fullname.toLowerCase().split(" ");
String[] initials = constraint.toLowerCase().split(" ");
if (initials.length > 1) {
   for (String nm : splitName) {
       for (String ini : initials) {
           if (nm.startsWith(ini)) { isMatch = true; } 
           else { isMatch = false; }
       }
   }
}
if (isMatch) { filteredContacts.add(contact); }

If I enter "J D", it only returns "Jane Doe" even though it should also display "John Doe". What's up with that?

4
  • "but I'm kind of stuck with this" is not a problem description. Please describe exactly what is and isn't working as expected and which errors you are getting. If you get exceptions then also include the stack trace. Commented Apr 15, 2015 at 6:04
  • If you want to avoid loops, you should consider implementing an index. Commented Apr 15, 2015 at 6:14
  • what is temp in your code? Commented Apr 15, 2015 at 8:26
  • sorry, it's contraint. I was playing around with my code a bit. Commented Apr 15, 2015 at 8:44

1 Answer 1

2

I would split the input using space character and check if result array is bigger than one. If yes, there are initials so you have to check them using String's startsWith() method.

String[] initials = constraint.split(" ");
if (initials.length == 1) {
    //single input, do you 'if' here
} else {
    //initials detected, check every string with 'startsWith()' method
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I played around with my code using this logic but somehow, something weird is happening to my filter. I've updated my code to show my additional filter.
Hard to say something more, but maybe you should add contact in a loop for every match. Now it looks like you are adding only the first one.
I was thinking that might be redundant. That entire snippet is inside the for (Contacts contact : unfilteredContacts) loop, i just add contact when my flag is true, but I will try to play around more with the code. Thanks for the suggestion.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.