问题描述
我有此代码(感谢@EpicPandaForce提供),删除时遇到问题.当我添加新联系人时,它就像一个超级按钮一样工作,而当我删除联系人(如果有两个,则从该联系人中删除该号码)时,它将保留在Realm中.如何使它正常工作?
I have this code (thankfully provided by @EpicPandaForce) and I have problem with deletion. When I add new contact it works like a charm while when I delete contact (or number from that contact if there are two of them) it stays persisted in Realm. How can I get it working properly?
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
Contact realmContact = new Contact();
String filter = "" + ContactsContract.Contacts.HAS_PHONE_NUMBER + " > 0 and "
+ ContactsContract.CommonDataKinds.Phone.TYPE +"="
+ ContactsContract.CommonDataKinds.Phone.TYPE_MAIN;
Cursor phones = getActivity()
.getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, filter, null, null);
while (phones.moveToNext()) {
String id = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
realmContact.setId(id);
realmContact.setName(name);
realmContact.setNumber(phoneNumber);
realmContact.setIsBeingSaved(true);
realm.insertOrUpdate(realmContact);
}
/** merge mechanism */
realm.where(Contact.class)
.equalTo("isBeingSaved", false)
.findAll()
.deleteAllFromRealm(); // delete all non-saved data
for(Contact contact : realm.where(Contact.class).findAll()) {
realmContact.setIsBeingSaved(false); // reset all save state
}
Contact.class
Contact.class
public class Contact extends RealmObject{
@PrimaryKey
private String id;
@Index
private String name;
@Index
private String number;
@Index
private boolean isBeingSaved;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public boolean getIsBeingSaved() {
return isBeingSaved;
}
public void setIsBeingSaved(boolean beingSaved) {
isBeingSaved = beingSaved;
}
}
编辑-工作代码:
Contact realmContact = new Contact();
Uri uri = Contacts.CONTENT_URI;
String selection = "((" + CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY + " NOTNULL) AND ("
+ Contacts.HAS_PHONE_NUMBER + "=1) AND ("
+ CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY + " != '' ))";
Cursor phones = getActivity()
.getContentResolver()
.query(uri, null, selection, null, null);
String phoneNumber = "";
while (phones.moveToNext()) {
String id = phones.getString(phones.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
String name = phones.getString(phones.getColumnIndex(Contacts.DISPLAY_NAME_PRIMARY));
String lastTimeContacted = phones.getString(phones.getColumnIndex(Contacts.LAST_TIME_CONTACTED));
if(Integer.parseInt(phones.getString(phones.getColumnIndex(Contacts.HAS_PHONE_NUMBER))) > 0){
Cursor pCur = getActivity().getContentResolver().query(
CommonDataKinds.Phone.CONTENT_URI,
null,
CommonDataKinds.Phone.LOOKUP_KEY +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
phoneNumber += "/" + pCur.getString(pCur.getColumnIndex(CommonDataKinds.Phone.NUMBER));
realmContact.setNumber(phoneNumber);
}
phoneNumber = "";
pCur.close();
} else {
realmContact.setNumber("1234");
}
realmContact.setId(id);
realmContact.setName(lastTimeContacted);
realmContact.setIsBeingSaved(true);
realm.insertOrUpdate(realmContact);
}
Log.i("asd-size", realm.where(Contact.class).findAll().size() + "");
/** merge mechanism */
realm.where(Contact.class)
.equalTo("isBeingSaved", false)
.findAll()
.deleteAllFromRealm(); // delete all non-saved data
for(Contact contact : realm.where(Contact.class).findAll()) {
contact.setIsBeingSaved(false); // reset all save state
}
推荐答案
好的,经过漫长的搜索,我发现有一个错字:
Okay, after quite long searching I found that there was a typo:
/** merge mechanism */
realm.where(Contact.class)
.equalTo("isBeingSaved", false)
.findAll()
.deleteAllFromRealm(); // delete all non-saved data
for(Contact contact : realm.where(Contact.class).findAll()) {
realmContact.setIsBeingSaved(false); <- here it is
}
实际上是contact
,它是realmContact
,因此它是指通过for
循环迭代的接触.
It's realmContact
when in fact it should be contact
so it refers to contact iterated by for
loop.
因此,基本上是将Contact realmContact = new Contact();
设置为false
.所以正确的版本是:
So, basically, it was setting Contact realmContact = new Contact();
to false
. So the proper version is:
/** merge mechanism */
realm.where(Contact.class)
.equalTo("isBeingSaved", false)
.findAll()
.deleteAllFromRealm(); // delete all non-saved data
for(Contact contact : realm.where(Contact.class).findAll()) {
contact.setIsBeingSaved(false);
}
这篇关于从内容提供者获取联系人(没有重复或无效联系人),并保存到Realm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!