我正在尝试解压缩我的联系人并对其进行更改,但问题是我的构建器似乎工作正常并且没有抛出任何异常,但没有任何更改!
Cursor c;
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Builder builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
builder.withSelection(ContactsContract.Data.CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + "=?", new String[]{String.valueOf(ContactID), ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE});
builder.withValue(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,"blabla");
ops.add(builder.build());
ContentProviderResult[] res;
try
{
res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
}
catch (Exception e)
{
e.printStackTrace();
}
setListViewAdapter() ;
c = getContentResolver().query(CommonDataKinds.Phone.CONTENT_URI, null, CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{ContactID}, null);
try {
c.moveToFirst();
displayName = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
}
finally
{
c.close();
}
这是我将适配器更新为联系人ListView的方法
private void setListViewAdapter() {
// Construct the data source
arrayOfUsers = getContactsData();
// update the adapter to convert the array to views
adapter.clear();
adapter.addAll(arrayOfUsers);
adapter.notifyDataSetChanged();
// Attach the adapter to a ListView
if (adapter!=null && lstContacts!=null)
{
try
{
lstContacts.setAdapter(adapter);
}
catch(Exception e) {
System.out.println("Error! "+e.getMessage());
}
}
else
NoContacts.setVisibility(View.VISIBLE);
}
这就是我从手机通讯录目录中获取联系人信息的方式,效果很好。
private ArrayList<Contact> getContactsData() {
String ID = "";
String name = "";
String number = "";
ArrayList <Contact> mycontacts = null;
try
{
//get our contact list via a Content Resolver which is the bridge between all android applications
Uri mContacts_uri= ContactsContract.Contacts.CONTENT_URI;
people = getContentResolver().query(mContacts_uri, // Contact URI
null, // Which columns to return
null, // Which rows to return
null, // Where clause parameters
null // Order by clause
);
int indexID = people.getColumnIndex(ContactsContract.Contacts._ID);
int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexhasnumbers = people.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
mycontacts = new ArrayList<Contact>();
Contact _person = null;
int cur = people.getCount();
if(cur>0)
{
people.moveToFirst();
do {
ID = people.getString(indexID);
name = people.getString(indexName);
/********************************************/
if (Integer.parseInt(people.getString(indexhasnumbers)) > 0)
{
Cursor pCur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{ID}, null);
try{
while (pCur.moveToNext()) {
int phoneType = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
switch (phoneType) {
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
Log.e(name + "(mobile number)", number);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
Log.e(name + "(home number)", number);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
Log.e(name + "(work number)", number);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER:
Log.e(name + "(other number)", number);
break;
default:
break;
}
}
pCur.close();
}
catch(Exception e)
{
Log.e("exception found : "+ e.getMessage()," : phone number ");
}
_person=new Contact(ID,name,number);
mycontacts.add(_person);
}
}
while (people.moveToNext());
}
}
catch(Exception e) {
System.out.println("Error! "+e.getMessage());
}
return mycontacts;
}
但是显示名称仍然相同,我不明白更新功能的哪个部分无法正常工作,因为没有人抛出异常
答案 0 :(得分:0)
使用此查询
String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
+ Contacts.HAS_PHONE_NUMBER + "=1) AND ("
+ Contacts.DISPLAY_NAME + " != '' ))";
Cursor c = cntx.getContentResolver().query(Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select,
null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
请参阅此链接,这可能会对您有所帮助
http://developer.android.com/reference/android/provider/ContactsContract.RawContacts.html