当我在适配器中过滤列表时,我从Realm数据库中获取列表时出现错误。这是我的ItemFilter类。
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String query = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final List<friend_model> list = original_items;
final List<friend_model> result_list = new ArrayList<>(list.size());
for (int i = 0; i < list.size(); i++) {
String str_title = list.get(i).getName();
if (str_title.toLowerCase().contains(query)) {
result_list.add(list.get(i));
}
}
results.values = result_list;
results.count = result_list.size();
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filtered_items = (List<friend_model>) results.values;
notifyDataSetChanged();
}
}
我收到的错误消息如
An exception occured during performFiltering()!
java.lang.IllegalStateException: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.
at io.realm.Realm.checkIfValid(Realm.java:191)
at io.realm.RealmResults.get(RealmResults.java:111)
at io.realm.RealmResults.get(RealmResults.java:55)
at com.mathi.finapp.adapters.Friend_adapter$ItemFilter.performFiltering(Friend_adapter.java:150)
at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.os.HandlerThread.run(HandlerThread.java:61)