2016-11-26 111 views
0

我有一个类如下:如何删除RealmList元素?

public class SomeStuff extends RealmObject { 

    public RealmList<CatDetails> cat; 


    public RealmList<CatDetails> getcat() { 
     return cat; 
    } 

    public void setCat(RealmList<CatDetails> cat) { 
     this.cat = cat; 
    } 
} 

和这里的CatDetails:

public class CatDetails extends RealmObject { 

    @Index String catNumber; 
    @Index String catName; 


    // setters and getters for catName and catNumber 
} 

现在我该怎样去从外地cat删除一个元素?也就是说,我想1

,以减少类SomeStuff ArrayList中的大小我已经试过:

realm.where(SomeStuff.class).findAll().get(pos).getCatDetails().get(0).deleteFromRealm(); 

还有:

realm.where(SomeStuff.class).findAll().get(pos).getCatDetails().deleteAllFromRealm(); 

,但我得到的在两种情况下填充回收站视图时的ArrayIndexOutOfBoundsException。

+0

那么你需要添加你在使用RecyclerView适配器所做的事情....无论是RealmRecyclerViewAdapter,更重要的是你在做什么线程删除 – EpicPandaForce

回答

0

删除:

realmList.get(0).deleteFromRealm(); 

但值得注意的是,表立即得到一个事务中所以只需使用增强的for循环不起作用像它在RealmResults更新。一个正常的iterator()remove()将工作。