2016-05-05 2 views
0

私はSearchViewでフィルタリングされたarrayAdapterを持っています。フィルタリングが働いているが、私の問題は、それはまた、編集すべきではない別のエンティティきれいということです:securitiesallSecurities:あなたは私が私のコンストラクタに2つのエンティティを持って見ることができるようにAndroid ArrayAdapter performFilterすべての値をクリーン

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; 
      } 
     }; 
    } 
} 

を。

securitiesは、アダプターをフィルするエンティティーであり、フィルターされたエンティティーです。

allSecuritiesは、ヘルパーエンティティです。フィルタリング後、フィルタから文字を削除すると、すべてのエンティティを取得してフィルタを再実行する必要がありますが、結果をフィルタリングしてsecuritiesに保存すると、フィルタリングされた結果はallSecuritiesになります。

私の質問はとても簡単です:なぜそれが編集のためにアクセスされない場合、私のエンティティを無効にするのですか?オーバーライドを避けるにはどうすればよいですか?

おかげで、すべての

答えて

2
this.allSecurities = securities; 

これはallSecuritiessecurities両方が同じArrayListを参照することを意味します。あなたが1つにすることは、他の人にも起こります。代わりに、ArrayListのコピーを作成する必要があります。

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

securitiesへの変更もallSecuritiesは変更されません。

+0

私は完全にメモリ参照を忘れてしまった、本当にありがとう! :) –

+0

(受け入れるのに5分必要です、私はそれを待っています) –

+0

よろしくお願いします。お役に立てて嬉しいです。 –

関連する問題