如何从适配器中的realm模型类获取数据

时间:2017-06-20 10:20:19

标签: android realm adapter

我希望在我的活动中添加搜索过滤器,其中数据在列表视图中显示,来自扩展RealmObject的模型类。但是在添加过滤器期间,它会在适配器类中抛出异常。

java.lang.IllegalStateException: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.

我理解了这个问题但是找不到任何出路来访问适配器类中的realm模型类数据?谢谢你的时间和帮助。这是适配器类代码

@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()))

上面是抛出异常的行,因为getShop_name()是领域模型类的方法

                    {
                        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();
    }

}

注意:当从不扩展RealmObject类的模型类访问数据时,添加搜索过滤器的代码工作正常

0 个答案:

没有答案