WhySoBizarreCode
WhySoBizarreCode

Reputation: 67

Filter of more than one type<Object>

Basically I want to filter with a customized filter an ArrayList that contain 2 types of item. <String> and <Titulo> and I can't get it.

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.

Upvotes: 0

Views: 102

Answers (1)

eslimaf
eslimaf

Reputation: 716

If I understood correctly you want to perform a filter if the object is of type Titulo, otherwise, just add it to the array. If that assumption is correct then you should check your object instance type (Titulo or 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
}

Upvotes: 1

Related Questions