为了账号安全,请及时绑定邮箱和手机立即绑定

如何在RealmRecyclerViewAdapter中实现可过滤

如何在RealmRecyclerViewAdapter中实现可过滤

繁星点点滴滴 2019-11-20 10:05:59
我正在使用RealmRecyclerViewAdapter。我的问题是实施Filterable不起作用。这是代码: private class AirportAdapter extends RealmRecyclerViewAdapter<AirportR,RecyclerView.ViewHolder> implements Filterable{    Context context;    OrderedRealmCollection<AirportR>listAirports;    public AirportAdapter(Context activity, OrderedRealmCollection<AirportR>airports)    {        super(activity,airports, true);        this.context = activity;        this.listAirports = airports;    }    @Override    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)    {        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.airport_show, parent,false);        AirportClass holder = new AirportClass(view);        return holder;    }    @Override    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)    {       AirportR airportR = listAirports.get(position);        AirportClass mHolder = (AirportClass)holder;        mHolder.country.setText(airportR.getIsoCountry());        mHolder.name.setText(airportR.getName());    }    public Filter getFilter()    {       AirportFilter filter = new AirportFilter(this, listAirports);        return filter;    }    private class AirportFilter extends Filter    {        private final AirportAdapter adapter;        OrderedRealmCollection<AirportR>originalList;        OrderedRealmCollection<AirportR>filteredList;        private AirportFilter(AirportAdapter adapter, OrderedRealmCollection<AirportR> originalList)        {            super();            this.adapter = adapter;            this.originalList = originalList;        }而我给了错误:java.lang.UnsupportedOperationException: This method is not supported by RealmResults.                  at io.realm.RealmResults.addAll(RealmResults.java:710)                  at com.example.matteo.downloadairports.fragment.ListAirportFragment$AirportAdapter$AirportFilter.publishResults过滤并更新适配器后如何保存结果?谢谢
查看完整描述

2 回答

?
繁星淼淼

TA贡献1775条经验 获得超11个赞

将过滤器移至publishResults并使用UI线程Realm的查询来评估新结果。


private class AirportAdapter

        extends RealmRecyclerViewAdapter<AirportR, RecyclerView.ViewHolder>

        implements Filterable {

    Realm realm;


    public AirportAdapter(Context context, Realm realm, OrderedRealmCollection<AirportR> airports) {

        super(context, airports, true);

        this.realm = realm;

    }


    @Override

    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.airport_show, parent, false);

        AirportClass holder = new AirportClass(view);

        return holder;

    }


    @Override

    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        AirportR airportR = getData().get(position);


        AirportClass mHolder = (AirportClass) holder;

        mHolder.bind(airportR);

    }


    public void filterResults(String text) {

        text = text == null ? null : text.toLowerCase().trim();

        RealmQuery<AirportR> query = realm.where(AirportR.class);

        if(!(text == null || "".equals(text))) {

            query.contains("fieldToQueryBy", text, Case.INSENSITIVE) // TODO: change field

        }

        updateData(query.findAllAsync());

    }


    public Filter getFilter() {

        AirportFilter filter = new AirportFilter(this);

        return filter;

    }


    private class AirportFilter

            extends Filter {

        private final AirportAdapter adapter;


        private AirportFilter(AirportAdapter adapter) {

            super();

            this.adapter = adapter;

        }


        @Override

        protected FilterResults performFiltering(CharSequence constraint) {

            return new FilterResults();

        }


        @Override

        protected void publishResults(CharSequence constraint, FilterResults results) {

            adapter.filterResults(constraint.toString());

        }

    }


    private class AirportClass

            extends RecyclerView.ViewHolder {

        TextView name, country;

        ImageView image;


        public AirportClass(View itemView) {

            super(itemView);


            name = (TextView) itemView.findViewById(R.id.name);

            country = (TextView) itemView.findViewById(R.id.country);

            image = (ImageView) itemView.findViewById(R.id.imageView);

        }


        public void bind(AirportR airportR) {            

            country.setText(airportR.getIsoCountry());

            name.setText(airportR.getName());

        }

    }

}


查看完整回答
反对 回复 2019-11-20
  • 2 回答
  • 0 关注
  • 532 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信