Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getcontentresolver () is undefined for the type

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

}

like image 775
mstfdz Avatar asked Aug 12 '13 08:08

mstfdz


1 Answers

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.

like image 75
Arash GM Avatar answered Nov 10 '22 13:11

Arash GM