2015-10-14 36 views
0

我想從RealmList(ideaList)向Recyclerview顯示一些RealmObjects(創意)。聽起來很簡單。但是,當我嘗試刪除對象,它雖然有當我做了查詢(正從一個特定用戶的所有想法)刪除在RecyclerView中顯示的RealmObject

代碼示例:

查詢:

public RealmList<Idea> getIdeaListFromRealm(Context ctx) { 
    realm = Realm.getInstance(ctx); 
    RealmQuery<Idea> ideaQuery = realm.where(Idea.class); 
    RealmResults<Idea> ideaQueryResults = ideaQuery.equalTo("owner.id",""+LoginFragment.loggedOwner.getId()).findAll(); 
    RealmList<Idea> ideaList = new RealmList<>(); 
    ideaList.addAll(ideaQueryResults); 
    return ideaList; 
} 

刪除(的onClick ) :

holder.delete.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      realm.beginTransaction(); 
      ideaList.remove(position); 
      notifyDataSetChanged(); 
      realm.commitTransaction(); 
      fragmentStateHandler.replaceFrag(Frag.ARCHIVEFRAGMENT); 
     } 
    }); 

我想我沒有正確刪除它。沒有其他原因,因爲在刪除後它仍然存在於查詢中。

我在做什麼錯了?在此先感謝

+0

任何人?........ –

回答

2

你是刪除對象只是從RealmList,它根本沒有堅持。

您必須刪除該對象要麼從結果中,要麼只是直接通過對象實例。例如

realm.beginTransaction(); 

ideaQueryResults.remove(position); 

// alternatively: 

Idea idea = ideaQueryResults.get(position); 
idea.removeFromRealm(); 

realm.commitTransaction(); 

// from the docs: it will always be more efficient to 
// use the more specific change events if you can. 
// Rely on notifyDataSetChanged() as a last resort. 
notifyItemRemoved(position); 
+1

我認爲你應該在你通知數據集之前提交。 – EpicPandaForce

+0

好點 - 編輯我的答案。 – marius

+0

哦。即時通訊如此愚蠢的哈哈.. 謝謝:)它的工作原理! –