getcontentresolver () is undefined for the type

偶尔善良 提交于 2019-12-09 17:53:52

问题


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

}


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/18182426/getcontentresolver-is-undefined-for-the-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!