2016-11-26 113 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()將工作。