gpt4 book ai didi

android - 更新后生日数据被删除

转载 作者:太空狗 更新时间:2023-10-29 13:36:26 25 4
gpt4 key购买 nike

我一直在尝试在我的联系人列表中添加/更新生日。我可以添加生日,但是更新生日时遇到问题。

更新后等待几秒钟,生日数据消失/被删除。

我注意到在更新生日之后,“dirty”字段被设置为“1”,我猜这会触发同步机制(我只是从联系人开始)

这是我正在使用的 fragment

插入/添加生日

    long rawContactId = -1;
String[] projection = new String[]{ContactsContract.CommonDataKinds.Event.RAW_CONTACT_ID};
String selection = ContactsContract.CommonDataKinds.Event.CONTACT_ID + "=?";

String[] selectionArgs = new String[]{
String.valueOf(bdayContact.getId())
};

Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
projection,
selection,
selectionArgs, null);
try {
if (c.moveToFirst()) {
rawContactId = c.getLong(0);
}
} finally {
c.close();
}

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Event.START_DATE, bday)
.withValue(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY)
.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();
}

更新生日

String selection = ContactsContract.CommonDataKinds.Event.CONTACT_ID+"=? AND " +
ContactsContract.CommonDataKinds.Event.MIMETYPE+"=? AND " +
ContactsContract.CommonDataKinds.Event.TYPE+"=?"
;
String[] selectionArgs = new String[]{
String.valueOf(contacts.get(position).getId()),
String.valueOf(ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE),
String.valueOf(ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY)
};

Cursor cursor = managedQuery(ContactsContract.Data.CONTENT_URI, null, selection, selectionArgs, null);

if(cursor.moveToFirst()){
int index = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event._ID);
String eventId = cursor.getString(index);

String bday = year+"-"+(monthOfYear+1)+"-"+dayOfMonth;
}

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withSelection(ContactsContract.Data._ID + " = ?", new String[] {eventId})
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Event.START_DATE, bday)
.withValue(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY)
.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();
}

最佳答案

我找到问题了!

数据因格式不正确而被删除。

看来月份和日期字段中的前导零很重要。不过我注意到一件事,即在插入新生日时,如果没有前导零并不重要。

String bday = year+"-"+String.format("%02d", (monthOfYear+1))+"-"+String.format("%02d", dayOfMonth);

关于android - 更新后生日数据被删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9996110/

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