【问题标题】:getcontentresolver () is undefined for the typegetcontentresolver () 未定义该类型
【发布时间】:2014-11-06 15:40:30
【问题描述】:

我尝试获取所有联系人姓名和电话号码,我正在尝试使用getContentResolver,但我得到了

方法获取内容解析器()未定义类型

这个错误。

我该如何解决?

下面是代码:

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

}

【问题讨论】:

    标签: android android-contentresolver


    【解决方案1】:

    问题是Context,传递你的Activity 的上下文,它在它的构造函数中使用你的Class

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

    然后使用

    context.getContentResolver()

    这里的上下文使用绝对完美。

    【讨论】:

      【解决方案2】:

      你也可以简单地使用这个:

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

      【讨论】:

        猜你喜欢
        • 2015-05-26
        • 1970-01-01
        • 2022-11-03
        • 1970-01-01
        • 2012-08-07
        • 2013-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多