getcontentresolver () is undefined for the type

mstfdz picture mstfdz · Aug 12, 2013 · Viewed 7.2k times · Source

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();
}

}

Answer

Arash GM picture Arash GM · Aug 12, 2013

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.