I have a problem with my custom filter. I make it, and it works well. When I debug code, it filtrates array well, but I have a problem in void publishResults(). I don't know what is a problem, anyone helps?
public class KatalogAdapter extends ArrayAdapter<Katalog> implements Filterable {
List<Katalog> object;
int num = 0;
public KatalogAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<Katalog> objects) {
super(context, resource, objects);
object = objects;
}
public void setObject(List<Katalog> kat){
this.object = kat;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View v = convertView;
if(v==null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.katalozi_item, null);
}
Katalog katalog = object.get(position);
ImageView katalogImage = (ImageView) v.findViewById(R.id.katalogImage);
TextView katalogProizvodjac = (TextView) v.findViewById(R.id.katalogProizvodjac);
TextView katalogVaziDo = (TextView) v.findViewById(R.id.katalogVaziDo);
TextView idKataloga = (TextView) v.findViewById(R.id.idKataloga);
String src = katalog.getImageSRC();
Picasso.with(getContext()).load(src).into(katalogImage);
/*Glide.with(getContext()).load(src).thumbnail(Glide.with(getContext()).load(R.drawable.loading_icon))
.fitCenter()
.crossFade().into(katalogImage); */
// katalogImage.setImageBitmap(bitmap);
katalogProizvodjac.setText(katalog.getKatalogProizvodjac());
String doe = katalog.getKatalogVaziDo();
char lastChar = doe.charAt(doe.length() - 1);
if(lastChar=='1'){
katalogVaziDo.setText("Važi još "+ doe +" dan");
}
if(doe.equals("0")){
katalogVaziDo.setText("Važi još danas");
}
if(lastChar!='1' && !doe.equals("0")){
katalogVaziDo.setText("Važi još "+katalog.getKatalogVaziDo() + " dana");
}
// katalogVaziDo.setText("Važi do: "+katalog.getKatalogVaziDo());
idKataloga.setText(String.valueOf(katalog.getIdKataloga()));
return v;
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
ArrayList<Katalog> tempList=new ArrayList<Katalog>();
//constraint is the result from text you want to filter against.
//objects is your data set you will filter from
if(constraint != null && object!=null) {
int length=object.size();
int i=0;
while(i<length){
Katalog item=object.get(i);
if(item.getKatalogProizvodjac().toLowerCase(Locale.getDefault()).contains(constraint.toString().toLowerCase())){
tempList.add(item);
}
i++;
}
//following two lines is very important
//as publish result can only take FilterResults objects
filterResults.values = tempList;
filterResults.count = tempList.size();
}
return filterResults;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
// object = (ArrayList<Katalog>) results.values;
setObject((List<Katalog>) results.values);
num = results.count;
notifyDataSetChanged();
}
};
}
}
I was looking for an answer, and I found that in that function I need to put in array my data. I did it, but still not work.