Question

I try to get all contact names and number and i'm trying to use getContentResolver but i am getting

the method get content resolver () is undefined for the type

this error.

How can i fix it ?

Here is the code below :

public class ContactManager  {

public ArrayList<Product> getContactNumber() {
    Cursor phones = getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
            null, null);
    while (phones.moveToNext()) {
        String name = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String phoneNumber = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    }
    phones.close();
}

}

Was it helpful?

Solution

The problem is Context, pass context of your Activity which use your Class in it's Constructor :

Context context;
public ContactManager (Context context) {
    this.context = context;
}

then use

context.getContentResolver()

absolutely perfect the use of context here.

OTHER TIPS

You can also simply use this:

 public class ContactManager  {

  public ArrayList<Product> getContactNumber(Context mContext) {
    Cursor phones = mContext.getContentResolver().query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
        null, null);
    while (phones.moveToNext()) {
    String name = phones
            .getString(phones
                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
    String phoneNumber = phones
            .getString(phones
                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
   phones.close();}}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top