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?