Android Contact phone number using Loader Invalid column data1

后端 未结 3 1870
日久生厌
日久生厌 2021-01-14 00:14

I used to use content resolver previously to fetch Contact list and their details while that still works I wanted to try the Loader method which queries the Content Provider

相关标签:
3条回答
  • 2021-01-14 00:20

    This is an excerpt of what I actually do in my (working) code.

    These are my relevant imports:

    import android.provider.ContactsContract;
    import android.provider.ContactsContract.CommonDataKinds.Email;
    import android.provider.ContactsContract.CommonDataKinds.Phone;
    import android.provider.ContactsContract.Contacts;
    

    Given that id is a string (representing a number, like "1", "20", ...)

    // Query for phone numbers for the selected contact id
    final Cursor cur = getContentResolver().query
        (
        Phone.CONTENT_URI, null,
        Phone.CONTACT_ID + "=?",
        new String[] {id}, null
        );
    
    final int phoneIdx = cur.getColumnIndex(Phone.NUMBER);
    final int phoneType = cur.getColumnIndex(Phone.TYPE);
    
    // ...
    
        if(cur.moveToFirst())
        {
            final String name =
                cur.getString
                (
                    cur.getColumnIndexOrThrow
                    (
                        ContactsContract.Contacts.DISPLAY_NAME
                    )
                );
        }
    

    Hope it puts you on the right path

    0 讨论(0)
  • 2021-01-14 00:43

    In my case the problem was with URI.

    I was using this:

    Uri uri = ContactsContract.Contacts.CONTENT_URI;
    

    and I changed for this (Phone.CONTENT_URI) to resolve the problem and obtain the phone number:

    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    

    This is the rest of the code:

    contactos = (TextView) findViewById(R.id.contactos);
    
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    
    String [] projection = new String[] {
                    ContactsContract.Contacts._ID,
                    ContactsContract.Contacts.DISPLAY_NAME,
                    ContactsContract.CommonDataKinds.Phone.NUMBER
            };
    
    Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
    
    while (cursor.moveToNext()){
                String nombre = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
                String telefono = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                contactos.append("Name: ");
                contactos.append(nombre);
                contactos.append(" - Phone Number: ");
                contactos.append(telefono);
                contactos.append("\n");
            }
    
    0 讨论(0)
  • 2021-01-14 00:46

    Change Your Query URI.

    You are using a URI that is not meant for searching/filtering those columns (e.g. phone numbers) on Contacts:

    You need to use a URI that has access to the columns you're trying to filter on, like this:

    Uri uri = ContactsContract.Data.CONTENT_URI;
    

    Or maybe this one since you're searching phone numbers:

    Uri uri = ContactsContract.PhoneLookup.CONTENT_FILTER_URI;
    

    There's a decent breakdown of what URIs to use and when on the Android SDK Documentation:

    • If you need to read an individual contact, consider using CONTENT_LOOKUP_URI instead of CONTENT_URI.

    • If you need to look up a contact by the phone number, use PhoneLookup.CONTENT_FILTER_URI, which is optimized for this purpose.

    • If you need to look up a contact by partial name, e.g. to produce filter-as-you-type suggestions, use the CONTENT_FILTER_URI URI.

    • If you need to look up a contact by some data element like email address, nickname, etc, use a query against the ContactsContract.Data table. The result will contain contact ID, name etc.

    0 讨论(0)
提交回复
热议问题