0

I have a list of Phone Numbers which I need to convert into a list of the corresponding Contact Name, so I have a method which checks each number in turn with the contact list and returns the name.

As the list is roughly 20 numbers long, when doing this there is a slight delay, maybe 1 1/2 seconds, which ideally I'd like to cut out. I was wondering if there was anything I can do to optimise my code?

I call the method getContactDisplayNameByNumber() like this:

ArrayList<String> tableCol1 = new ArrayList<String>();

for (int j = 0; j < tableCol1.size(); j++) {
        String name = getContactDisplayNameByNumber(tableCol1.get(j));
        tableCol1.set(j, name);
        }

And here is the method:

public String getContactDisplayNameByNumber(String number) {
    Uri uri = Uri.withAppendedPath(
            ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
            Uri.encode(number));
    String name = "?";

    ContentResolver contentResolver = getContentResolver();
    Cursor contactLookup = contentResolver.query(uri, new String[] {
            BaseColumns._ID, ContactsContract.PhoneLookup.DISPLAY_NAME },
            null, null, null);

    try {
        if (contactLookup != null && contactLookup.getCount() > 0) {
            contactLookup.moveToNext();
            name = contactLookup.getString(contactLookup
                    .getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
        }
    } finally {
        if (contactLookup != null) {
            contactLookup.close();
        }
    }

    return name;
}

Would it cut down the delay at all if I put the for loop inside the method, and made it a method for returning all names rather than just one?

Matt Harris
  • 3,496
  • 5
  • 32
  • 43

1 Answers1

1

I believe there can be two issues for your problem.

i) Maybe you are not using the optimal method to make the query. As I myself have not done that all I can ask you is to maybe look at this question to confirm. How to read contacts on Android 2.0

ii) If that is not the problem then maybe you can give this a shot. Why not sort the numbers in a particular order(say increasing). Get all the contacts.Also order the query results over the phone number field using the order by clause in the same order.Now try to check for the first number in your list. If it is found at index i in the query results then you can be sure that the next number on your list shall only be found at an index greater than i in the query results. You don't have to iterate over the previous i-1 query results. Order reduced from n^n to nlogn. Also this way you make only one query on the contacts

P.S. IMHO on such a small data set I believe the main problem of performance is the multiple number of queries.

Community
  • 1
  • 1
ShivangSeth
  • 72
  • 1
  • 8
  • Thanks for your answer. The numbers are already sorted in increasing order, so I will try to implement it that way, so that it queries for all the contacts at the same time. – Matt Harris Jan 28 '12 at 00:32