如何在RealmRecyclerViewAdapter中实现可过滤

我正在使用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

过滤并更新适配器后如何保存结果?


谢谢


繁星点点滴滴
浏览 533回答 2
2回答

繁星淼淼

将过滤器移至publishResults并使用UI线程Realm的查询来评估新结果。private class AirportAdapter&nbsp; &nbsp; &nbsp; &nbsp; extends RealmRecyclerViewAdapter<AirportR, RecyclerView.ViewHolder>&nbsp; &nbsp; &nbsp; &nbsp; implements Filterable {&nbsp; &nbsp; Realm realm;&nbsp; &nbsp; public AirportAdapter(Context context, Realm realm, OrderedRealmCollection<AirportR> airports) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, airports, true);&nbsp; &nbsp; &nbsp; &nbsp; this.realm = realm;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {&nbsp; &nbsp; &nbsp; &nbsp; View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.airport_show, parent, false);&nbsp; &nbsp; &nbsp; &nbsp; AirportClass holder = new AirportClass(view);&nbsp; &nbsp; &nbsp; &nbsp; return holder;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {&nbsp; &nbsp; &nbsp; &nbsp; AirportR airportR = getData().get(position);&nbsp; &nbsp; &nbsp; &nbsp; AirportClass mHolder = (AirportClass) holder;&nbsp; &nbsp; &nbsp; &nbsp; mHolder.bind(airportR);&nbsp; &nbsp; }&nbsp; &nbsp; public void filterResults(String text) {&nbsp; &nbsp; &nbsp; &nbsp; text = text == null ? null : text.toLowerCase().trim();&nbsp; &nbsp; &nbsp; &nbsp; RealmQuery<AirportR> query = realm.where(AirportR.class);&nbsp; &nbsp; &nbsp; &nbsp; if(!(text == null || "".equals(text))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; query.contains("fieldToQueryBy", text, Case.INSENSITIVE) // TODO: change field&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; updateData(query.findAllAsync());&nbsp; &nbsp; }&nbsp; &nbsp; public Filter getFilter() {&nbsp; &nbsp; &nbsp; &nbsp; AirportFilter filter = new AirportFilter(this);&nbsp; &nbsp; &nbsp; &nbsp; return filter;&nbsp; &nbsp; }&nbsp; &nbsp; private class AirportFilter&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; extends Filter {&nbsp; &nbsp; &nbsp; &nbsp; private final AirportAdapter adapter;&nbsp; &nbsp; &nbsp; &nbsp; private AirportFilter(AirportAdapter adapter) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.adapter = adapter;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected FilterResults performFiltering(CharSequence constraint) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new FilterResults();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected void publishResults(CharSequence constraint, FilterResults results) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adapter.filterResults(constraint.toString());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private class AirportClass&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; extends RecyclerView.ViewHolder {&nbsp; &nbsp; &nbsp; &nbsp; TextView name, country;&nbsp; &nbsp; &nbsp; &nbsp; ImageView image;&nbsp; &nbsp; &nbsp; &nbsp; public AirportClass(View itemView) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(itemView);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name = (TextView) itemView.findViewById(R.id.name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; country = (TextView) itemView.findViewById(R.id.country);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; image = (ImageView) itemView.findViewById(R.id.imageView);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void bind(AirportR airportR) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; country.setText(airportR.getIsoCountry());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name.setText(airportR.getName());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android