使用更新的搜索结果列表搜索和更新RealmBaseAdapter的一般过程是什么?
我有以下代码:
public void searchVideos(String searchText)
{
RealmResults<TutorialModel> results = realm.where(TutorialModel.class).contains("title",
searchText, Case.INSENSITIVE).findAll();
videoListAdapter.updateRealmResults(results);
videoListAdapter.notifyDataSetChanged();
}
但是我的单元格列表没有更新以显示搜索结果。
我正在使用RealmBaseAdapter和GridView。我的videoListAdapter
扩展了RealmBaseAdapter
。
我做错了吗?
此处的文档:
https://realm.io/docs/java/latest/
甚至不包含与“updateRealmResults”匹配的单个单词。
我希望我不必诉诸:
RealmResults<TutorialModel> results = realm.where(TutorialModel.class).contains("title",
searchText, Case.INSENSITIVE).findAll();
videoListAdapter = new VideoListAdapter(this, results, true);
tblVideos.setAdapter(videoListAdapter);
如果必须,那会很糟糕。
请告诉我有更合适的方式...
我也试过了:
realm.beginTransaction();
arrItems.clear();
realm.commitTransaction();
RealmResults<TutorialModel> results = realm.where(TutorialModel.class).contains("title",
searchText, Case.INSENSITIVE).findAll();
videoListAdapter.updateRealmResults(results);
videoListAdapter.notifyDataSetChanged();
但是,Realm结果似乎在某种程度上是链接的,这意味着如果我去arrItems.clear()
它会清除所有搜索查询中的结果......对接中的痛苦......导致任何后续的RealmResults返回0结果。
答案 0 :(得分:0)
我意识到自己的错误。
我的VideoListAdapter
原来是BaseAdapter
,因此我在BaseAdapter中使用了一个名为arrItems
的变量。
自从我将VideoListAdapter类更改为RealmBaseAdapter
后,我没有意识到我还需要更改适配器以使用已定义的域this.realmResults
所以我之前有这样的事情:
public VideoListAdapter(Context context, RealmResults<TutorialModel> realmResults, boolean
automaticUpdate)
{
super(context, realmResults, automaticUpdate);
this.context = context;
this.arrItems = realmResults;
}
应该是:
public VideoListAdapter(Context context, RealmResults<TutorialModel> realmResults, boolean
automaticUpdate)
{
super(context, realmResults, automaticUpdate);
this.context = context;
this.realmResults = realmResults;
}
并使用this.realmResults
通过适配器委托方法(例如this.arrItems
,getItemCount()
等)使用getView()
。
:d
我的活动更新中的新代码:
public void searchVideos(String searchText)
{
// -------------------------------------------------------------
// note: this arrItems is my Activity's arrItems,
// not the adapter's this.realmResults
// -------------------------------------------------------------
arrItems = realm.where(TutorialModel.class).contains("title",
searchText, Case.INSENSITIVE).findAll();
videoListAdapter.updateRealmResults(arrItems);
}
我又是一个幸福的家伙。