添加自定义字段,一个电话号码 [英] Add a custom field to a phone number

查看:116
本文介绍了添加自定义字段,一个电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个应用程序需要自定义联系人字段('白名单',如果你想知道)对每一个电话号码。

Am building an app that requires a custom contact field ('whitelist', if you were wondering) for every phone number.

不过,我只找到保存自定义的数据为每个联系人,与 Data.RAW_CONTACT_ID 的一种方式,但不是每个电话号码。我试图用 Phone._id ,但我得到了一个 java.nullpointerexception 错误。

However, I only found a way of saving custom data for each contact, with Data.RAW_CONTACT_ID, but not for each phone number. I tried using Phone._id, but I got a java.nullpointerexception error.

这是在code我现在有:

This is the code I have now:

try{ //phoneId = get Phone._ID from cursor
  ContentValues values = new ContentValues();                           
  values.put(Data.DATA1, "yes");
  int state = getContentResolver().update(Phone.CONTENT_URI, values, Phone._ID + " = " 
              + phoneId + " AND " +  Data.MIMETYPE + "='" + MIMETYPE_WHITELIST_CONTACT+"'", null);
         if (state == 0) {
             values.put(Phone._ID, phoneId);
             values.put(Data.DATA1, "yes");
             values.put(Data.MIMETYPE, MIMETYPE_WHITELIST_CONTACT);
                            getContentResolver().insert(Data.CONTENT_URI, values);
        }  
}catch (Exception e) {
    Toast.makeText(ChooseContactsActivity.this,  e.toString(), Toast.LENGTH_LONG).show();
}

没有任何人有任何想法需要改变什么,因此可以保存自定义字段的电话号码,而不是接触?

Does anyone have any idea what needs to be changed so it can save a custom field for the phone number, not the contact?

推荐答案

好吧,我理解了它。也许有更好的解决方案,但这个工作对我来说:

All right, I figured it out. Maybe there is a better solution, but this works for me:

    values.put(Data.RAW_CONTACT_ID, contactId);
    values.put(Data.DATA1, phoneId);
    values.put(Data.DATA2, "1");
    values.put(Data.DATA5, phoneNum);
    values.put(Data.MIMETYPE, MIMETYPE_WHITELIST_CONTACT);
getContentResolver().insert(Data.CONTENT_URI, values);

当我询问,我只需要添加phoneId得到的结果,我需要:

When I query, I just have to add the phoneId to get the result I need:

getContentResolver().query(Data.CONTENT_URI, {Data.DATA1, Data.DATA2},
 Data.RAW_CONTACT_ID + " = " + contactId +  " AND "  + 
    Data.DATA1 + " = " + phoneId + " AND "  +
            Data.MIMETYPE + "='" + MIMETYPE_WHITELIST_CONTACT+"'", null, null);

这篇关于添加自定义字段,一个电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆