简体   繁体   English

多种过滤器<Object>

[英]Filter of more than one type<Object>

Basically I want to filter with a customized filter an ArrayList that contain 2 types of item. 基本上,我想使用自定义过滤器过滤包含2种项目的ArrayList。 <String> and <Titulo> and I can't get it. <String><Titulo> ,我听不懂。

The error is that with the current method when I press some key just the filter return null view. 错误是,当我按某个键时,当前方法只是过滤器返回空视图。 But when I do the same just with one type in the filter all go good. 但是,当我只对过滤器中的一种进行相同操作时,一切都会顺利进行。

 class customFilter extends Filter{
            @Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();

    if(constraint!=null && constraint.length()>0){ 

     constraint=constraint.toString().toUpperCase();
     ArrayList<Object> filter = new ArrayList<>();

     for(int i = 0; i<tempArray.size();i++) {
    if(((titulo)tempArray.get(i)).getTituloTitulo().toUpperCase().contains(constraint)) {

 titulo Titulo = new titulo(((titulo) tempArray.get(i)).getnTitulo(),((titulo) tempArray.get(i)).getTituloTitulo());

filter.add(Titulo);

                         }

     if(((String) tempArray.get(i)).toUpperCase().contains("LIBRO")){
    filter.add(tempArray.get(i));
      }
    }

    results.count = filter.size();
    results.values=filter;

    }else{
     results.count=tempArray.size();
     results.values=tempArray;

 }

    return results;
  }

And the publish 和发布

    @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
       originalArray=(ArrayList<Object>)results.values;
       notifyDataSetChanged();

            }

I just want to get all the Items of type <titulo> get filtered and all the Items of type string conserve the position in order to keep all the ArrayList in order. 我只想获取所有<titulo>类型的项目,并过滤所有类型为string的项目,以保持所有ArrayList的位置。

If I understood correctly you want to perform a filter if the object is of type Titulo, otherwise, just add it to the array. 如果我正确理解,如果对象是Titulo类型,则要执行过滤,否则,只需将其添加到数组即可。 If that assumption is correct then you should check your object instance type (Titulo or String). 如果该假设正确,则应检查对象实例类型(Titulo或String)。 But you are actually performing a casting. 但是您实际上正在执行转换。

if(tempArray.get(i) instanceOf Titulo){
   //filter your titles here
} else {
  // add your String to the array
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM