I have an arrayAdapter which is filtered with a SearchView. The filtering is working, but my problem is that it also clean another entity which shouldn't be edited:
public class LvSecurityAdapter extends ArrayAdapter<Security> implements Filterable{
private ArrayList<Security> allSecurities;
private ArrayList<Security> securities;
private Context ctx;
public LvSecurityAdapter(Context ctx, int tvResourceInt, ArrayList<Security> securities) {
super(ctx, tvResourceInt, securities);
this.securities = securities;
this.allSecurities = securities;
this.ctx = ctx;
}
@Override
public int getCount() {
return this.securities.size();
}
@SuppressLint("InflateParams")
@Override
public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.row_lv_security, null);
Security security = securities.get(position);
final ImageView img = (ImageView) view.findViewById(R.id.ivTipologia);
final TextView tvName = (TextView) view.findViewById(R.id.tvName);
final TextView tvDescription = (TextView) view.findViewById(R.id.tvDescription);
final TextView tvTags = (TextView) view.findViewById(R.id.tvTags);
img.setBackground(ctx.getDrawable(security.getTipology().getImageId()));
tvName.setText(security.getName());
tvDescription.setText(security.getDescription());
tvTags.setText(security.getTag());
return view;
}
private void notifyThis(ArrayList<Security> values){
securities.clear();
securities.addAll(values);
this.notifyDataSetChanged();
}
@Override
public Filter getFilter() {
return new Filter(){
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
notifyThis((ArrayList<Security>)results.values);
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
ArrayList<Security> securitiesFilteredLocal = new ArrayList<>();
for (Security s : allSecurities){
if(s.getName().toLowerCase().contains(constraint.toString().toLowerCase()) ||
s.getDescription().toLowerCase().contains(constraint.toString().toLowerCase())||
s.getTag().toLowerCase().contains(constraint.toString().toLowerCase())||
s.getTipology().getName().toLowerCase().contains(constraint.toString().toLowerCase())){
securitiesFilteredLocal.add(s);
}
}
results.count = securitiesFilteredLocal.size();
results.values = securitiesFilteredLocal;
return results;
}
};
}
}
As you can see I have two entities on my constructor: securities and allSecurities.
securities is the entity which fills the adapter and the one which is filtered.
allSecurities is an helper entity. I need it because after filtering, if I delete some characters from the filter, I need to retrieve all entities and perform the filter again, but when I filter my results and I save them into securities, it also put the filtered result into allSecurities.
My question is pretty simple: why it override my entity if it is never accessed for edits? and how can I avoid the overriding?
Thanks all