0

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

1 Answer 1

2
this.allSecurities = securities;

This means that allSecurities and securities both reference the same ArrayList. Whatever you do to one will also happen to the other. What you should do instead is make a copy of the ArrayList.

this.allSecurities = new ArrayList<>(securities);

Now any modifications to securities will not also modify allSecurities.

Sign up to request clarification or add additional context in comments.

1 Comment

I totally forgot about memory ref, thanks really a lot!! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.