我添加了一个单个用户的数据,就像这样:
Realm realm = Realm.getDefaultInstance();
newUser = new UserDatabase();
realm.executeTransaction(new Realm.Transaction() {
public void execute(Realm realm) {
newUser.setClassName(classSectionName);
newUser.setClassNO(classNO);
newUser.setImageUrl(imageUrl);
newUser.setRollNo(rollNO);
newUser.setSchool(school);
// and now saved the data in peresistant data like this:
realm.copyToRealm(newUser);
}
});
但是我无法找到从Realm数据库中删除单个条目的方法,即使我尝试了这样的方式,它也不起作用。
Realm realm = Realm.getDefaultInstance();
UserDatabase tempUser = new UserDatabase();
final RealmResults<UserDatabase> students = realm.where(UserDatabase.class).findAll();
for(int i=0 ; i<students.size();i++){
final int index =i;
if((students.get(i).getUserID()).equals(prefs.getString(QRActivity.USER_ID_AFTER_LOGIN,"jpt"))&&
(students.get(i).getUserName()).equals(prefs.getString(QRActivity.USER_NAME_AFTER_LOGIN,"jpt"))){
realm.executeTransaction(new Realm.Transaction() {
public void execute(Realm realm) {
//Trying to delete a row from realm.
students.deleteFromRealm(index);
}
});
}
}
有人有什么想法吗?