Android: ArrayAdapter filled from a cursor

Status
Not open for further replies.
I need to fill an array adapter from a cursor.
Code:
Cursor cursor = getContentResolver().query(Contacts.CONTENT_URI,null, null, null, Contacts.DISPLAY_NAME);

So I create the cursor, loop through it, read two columns from it and make a new string out of it, then add it to the adapter. This works, for certain values of "work", but it works about ten thousand times slower than I expect. On my coworker's phone, it takes 16 seconds to read maybe fifty contacts.

Why?
 

Mile Zero

Ars Praetorian
578
Subscriptor++
Tiiba":2o1dtfzi said:
I need to fill an array adapter from a cursor.
Code:
Cursor cursor = getContentResolver().query(Contacts.CONTENT_URI,null, null, null, Contacts.DISPLAY_NAME);

So I create the cursor, loop through it, read two columns from it and make a new string out of it, then add it to the adapter. This works, for certain values of "work", but it works about ten thousand times slower than I expect. On my coworker's phone, it takes 16 seconds to read maybe fifty contacts.

Why?

Can we see a little more code than that? The whole loop would help.

Is there a reason you're using the Contacts.CONTENT_URI, instead of the Contacts.People.CONTENT_URI to get that specific subtable? The Contacts dataprovider contains a LOT of information, maybe you're asking the cursor for more than you think you are.

For reference, the code for Google's own Cupcake-era Contacts application is here.
 
"Is there a reason you're using the Contacts.CONTENT_URI, instead of the Contacts.People.CONTENT_URI to get that specific subtable? "

It's deprecated. And it points to ContactsContract, and it doesn't have a CONTENT_URI, which, as I understand, is what you need to get all the phones.

private int getContactsPhones(ArrayAdapter a)
{
int out = 0;
try
{
Cursor cursor = getContentResolver().query(
Contacts.CONTENT_URI,
null, null, null, Contacts.DISPLAY_NAME);
while (cursor.moveToNext())
{
String contactId = cursor.getString(cursor.getColumnIndex(
Contacts._ID));
String hasPhone = cursor.getString(cursor.getColumnIndex(
Contacts.HAS_PHONE_NUMBER));
String name = cursor.getString(cursor.getColumnIndex(
Contacts.DISPLAY_NAME));
if (hasPhone.equals("1") || hasPhone.equalsIgnoreCase("true"))
{
Cursor phones = getContentResolver().query(Phone.CONTENT_URI,
null, Phone.CONTACT_ID + " = " + contactId, null,
null);
while (phones.moveToNext())
{
int typeI = phones.getInt(
phones.getColumnIndex(Phone.TYPE));
char typeC;
switch (typeI)
{
case Phone.TYPE_HOME:
typeC = 'H';
break;
case Phone.TYPE_MOBILE:
typeC = 'M';
break;
case Phone.TYPE_WORK:
typeC = 'W';
break;
default:
typeC = '?';
}
String num =
phones.getString(phones.getColumnIndex(
Phone.NUMBER));
StringBuilder sb = new StringBuilder();
for (char c : num.toCharArray())
{
if (c >= '0' && c <= '9')
{
sb.append(c);
out++;
}
}
a.add(name + ": " + sb + " (" + typeC + ")");
}
phones.close();
}
}
cursor.close();
return out;
}
catch (Exception e)
{
return -1;
}
}
 

Mile Zero

Ars Praetorian
578
Subscriptor++
Tiiba":29iash01 said:
"Is there a reason you're using the Contacts.CONTENT_URI, instead of the Contacts.People.CONTENT_URI to get that specific subtable? "

It's deprecated. And it points to ContactsContract, and it doesn't have a CONTENT_URI, which, as I understand, is what you need to get all the phones.

Yeah, but so is the Contacts.CONTENT_URI. Both of them should be ignored in favor of ContactsContract in SDK level 5+, because the latter allows you to take advantage of additional sync adapters like the Facebook phone list. There's definitely a CONTENT_URI for that, and the process is similar to what you're doing already--get the contact, then pull the phone numbers from the raw data table.

However, if you're targeting SDK levels lower than 5, it would seem to make sense to use the People URI, and then query the Phones data via the People.Phones URI subdirectory.
 

Mile Zero

Ars Praetorian
578
Subscriptor++
Tiiba":25o7j9um said:
(using android.provider.ContactsContract.CommonDataKinds.Email)
Code:
getContentResolver().query(Email.CONTENT_URI,
                    new String[]
                    {
                        Email._ID, Email.DATA, Email.DISPLAY_NAME
                    }, null, null, null);

I haven't tested the speed yet, but why does Email.DISPLAY_NAME not have any data in it? I found that data lives in "display_name", while Email.DISPLAY_NAME = "data4". x="display_name"; find x?

As I understand it, the ContactsContract data table stores e-mail, phone, and other contact data in a table with numbered columns (data1 - data15) that can mean different things based on MIME type. The data4 column referenced by Email.DISPLAY_NAME may be used by sync adapters, but it's not used by the default contact application (I double-checked by peeking at the table in sqlite, on the emulator in data/data/com.android.providers.contacts/databases/contacts2.db), and you're probably better off just going with Email.DATA and getting a DISPLAY_NAME (if one exists) from the People query.
 
Status
Not open for further replies.