Android Contact phone number using Loader Invalid column data1

后端 未结 3 1911
日久生厌
日久生厌 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: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");
            }
    

提交回复
热议问题