I want to add search filter in my activity where data is being displayed in a listview, from a model class which extends RealmObject. But during the addition of filter it throws following exception in adapter class.
java.lang.IllegalStateException: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.
I understood the problem but couldn't find any way out to access realm model class data in adapter class? Thanks for you time and help. Here is the adapter class code
@Override
public Filter getFilter()
{
if(valueFilter == null)
{
valueFilter = new ValueFilter();
}
return valueFilter;
}
private class ValueFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint)
{
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0)
{
ArrayList<ShopModel> filterList = new ArrayList<>();
for (int i = 0; i < copyList.size(); i++)
{
if ((copyList.get(i).getShop_name().toLowerCase())
.contains(constraint.toString().toLowerCase()))
Above is line where it throws exception as getShop_name() is method of realm model class
{
filterList.add(copyList.get(i));
}
}
results.count = filterList.size();
results.values = filterList;
}
else
{
results.count = copyList.size();
results.values = copyList;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
arrayList = (ArrayList<ShopModel>) results.values;
notifyDataSetChanged();
}
}
Note : this code for adding search filter is working fine when data is accessed from a model class which doesn't extend RealmObject class