gpt4 book ai didi

android - 向联系人添加自定义类型的号码 [Android]

转载 作者:行者123 更新时间:2023-11-29 00:42:51 24 4
gpt4 key购买 nike

我一直在开发一款可以帮助向您的联系人发送短信的应用程序。一切都很好,除了我应该向“工作”或“私有(private)”等联系人添加“自定义字段编号”。我会在网上搜索答案,而那些对每个人都有用的答案对我来说并不适用。这是我的代码:

private void AddCtxtAttribute(int contactID, String contactNumber) {

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

ContentProviderOperation.Builder builder = ContentProviderOperation
.newInsert(Data.CONTENT_URI);

builder.withValue(Data.RAW_CONTACT_ID, contactID);
builder.withValue(ContactsContract.Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
builder.withValue(ContactsContract.Data.DATA1, contactNumber);
builder.withValue(ContactsContract.Data.DATA2, Phone.TYPE_CUSTOM);
builder.withValue(ContactsContract.Data.DATA3, "My Custom Label");

ops.add(builder.build());

try {
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
Log.e("RESULT", "Success! " + contactID + " - "
+ contactNumber);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("ERROR", "error : " + e.toString());
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("ERROR", "error : " + e.toString());
}

}

它叫做:

AddCtxtAttribute(contacto_id, contacto_numero);

其中 contacto_id 和 contacto_numero 分别是一个 int 和一个 String。

问题是当我按下按钮时,我有那个联系人的 ID (contacto_id) 但它会更新其他联系人。 (比如 id 不匹配)但我调试了它并且 id 没有改变。

谁能帮我解决这个问题?

最佳答案

我想通了!

问题是我在读取联系人和插入新联系人号码时使用了不同的列名和 URI。

我这样做了:查询所有联系人:

private void llenar_contactos() {

ProgressDialog progress;
lista_contactos_cel_sobrantes = null;

sobrantes = false;

lista_contactos_cel = new ArrayList<HashMap<String, Object>>();

progress = ProgressDialog.show(contactsActivity.this, "",
"Cargando Contactos. Porfavor espere...");

String[] projection = new String[] { Data.RAW_CONTACT_ID,
Phone.DISPLAY_NAME, Phone.NUMBER, Phone.LABEL };

String selection = Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'";

// ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER + "='1'";
String sort_order = "display_name ASC";

Uri mContacts = Data.CONTENT_URI;
// ContactsContract.CommonDataKinds.Phone.CONTENT_URI;

Cursor contacts = getContentResolver().query(mContacts, // Contact URI
projection, // Which columns to return
selection, // Which rows to return
null, // Where clause parameters
sort_order // Order by clause
);

total_contactos = contacts.getCount();

if (total_contactos > 0) {

int i = 0;

int multiplo = 0;
int indice_total = 0;

int temp_registros = 0;

String contact_id = "";
String name = "";
String phoneNo = "";
String label = "";
sobrantes = false;

int nameFieldColumnIndex = 0;

while (contacts.moveToNext()) {
nameFieldColumnIndex = contacts.getColumnIndex(Data.RAW_CONTACT_ID);

if (nameFieldColumnIndex > -1) {
contact_id = contacts.getString(nameFieldColumnIndex);
}

nameFieldColumnIndex = contacts.getColumnIndex(Phone.DISPLAY_NAME);

if (nameFieldColumnIndex > -1) {
name = contacts.getString(nameFieldColumnIndex);
}

nameFieldColumnIndex = contacts.getColumnIndex(Phone.NUMBER);

if (nameFieldColumnIndex > -1) {
phoneNo = contacts.getString(nameFieldColumnIndex);
}

nameFieldColumnIndex = contacts.getColumnIndex(Phone.LABEL);

if (nameFieldColumnIndex > -1) {
label = contacts.getString(nameFieldColumnIndex);
}

if(label != null){
Log.i("CONTACTO", "id: " + contact_id + " -> name: " + name
+ " ->number: " + phoneNo + " ->label: " + label);
} else {
Log.d("CONTACTO", "id: " + contact_id + " -> name: " + name
+ " ->number: " + phoneNo + " ->label: " + label);
}


}
contacts.close();
}
}

并插入一个新的自定义标签:

private void AddCtxtAttribute(int contactID, String contactNumber) {

if (contactID != 0 && contactNumber != null) {

ArrayList<ContentProviderOperation> ops =
new ArrayList<ContentProviderOperation>();

ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValue(Data.RAW_CONTACT_ID, contactID)
.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.NUMBER, contactNumber)
.withValue(Phone.TYPE, Phone.TYPE_CUSTOM)
.withValue(Phone.LABEL, "My Custom Label")
.build());
try{
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

关于android - 向联系人添加自定义类型的号码 [Android],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8371272/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com