5

잘 RealmRecyclerViewAdapter를 사용하고 있습니다. 내 문제는 Filterable 작동하지 않는 구현하는 것입니다. 이 코드입니다 :RealmRecyclerViewAdapter에서 필터링 기능을 구현하는 방법

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; 

     } 


     @Override 
     protected FilterResults performFiltering(CharSequence constraint) 
     { 
      filteredList.clear(); 
      final FilterResults results = new FilterResults(); 

      if (constraint.length() == 0) 
      { 
       filteredList.addAll(originalList); 
      } 
      else 
      { 
       final String filterPattern = constraint.toString().toLowerCase().trim(); 

       for (final AirportR airportR : originalList) 
       { 
        if (airportR.getName().toLowerCase().contains(filterPattern)) 
        { 
         filteredList.add(airportR); 
        } 
       } 
      } 
      results.values = filteredList; 
      results.count = filteredList.size(); 
      return results; 
     } 

     @Override 
     protected void publishResults(CharSequence constraint, FilterResults results) 
     { 
      adapter.listAirports.addAll((OrderedRealmCollection<AirportR>) results.values); 
      adapter.notifyDataSetChanged(); 
     } 
    } 



    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); 

     } 
    } 
} 

나는 오류 돌려 줄 :

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 

어떻게 내가 그것을 필터링하고 어댑터를 업데이트 한 후 그 결과를 구할 수 있습니까?

감사

답변

6
publishResults에 필터링을 이동하고 새로운 결과를 평가하기 위해 UI 스레드 영역의 쿼리를 사용

.

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.country.setText(airportR.getIsoCountry()); 
     mHolder.name.setText(airportR.getName()); 
    } 

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

    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); 
     } 
    } 
} 
+1

멋진 작품입니다! 당신의 소중한 도움에 감사드립니다! – ste9206

+0

다행스럽게도 렐름 사용법을 간소화 할 수 있습니다. p – EpicPandaForce

+0

@EpicPandaForce - updateData (listAirports)를 보여 주시겠습니까? 어떻게하면 필터 전후에 listview 항목을 업데이트 할 수 있습니까? – user2269164