Android ArrayAdapter performFilter 清除所有值
Android ArrayAdapter performFilter clean all values
我有一个用 SearchView 过滤的 arrayAdapter。过滤有效,但我的问题是它还清除了另一个不应编辑的实体:
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
是一个辅助实体。我需要它,因为过滤后,如果我从过滤器中删除一些字符,我需要检索所有实体并再次执行过滤,但是当我过滤结果并将它们保存到 securities
时,它也会将过滤后的内容放入结果为 allSecurities
.
我的问题很简单:如果从未访问它进行编辑,为什么它会覆盖我的实体?我怎样才能避免覆盖?
谢谢大家
this.allSecurities = securities;
这意味着 allSecurities
和 securities
都引用相同的 ArrayList
。无论你对一个人做什么,都会发生在另一个人身上。您应该做的是复制 ArrayList
.
this.allSecurities = new ArrayList<>(securities);
现在对 securities
的任何修改都不会同时修改 allSecurities
。
我有一个用 SearchView 过滤的 arrayAdapter。过滤有效,但我的问题是它还清除了另一个不应编辑的实体:
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
是一个辅助实体。我需要它,因为过滤后,如果我从过滤器中删除一些字符,我需要检索所有实体并再次执行过滤,但是当我过滤结果并将它们保存到 securities
时,它也会将过滤后的内容放入结果为 allSecurities
.
我的问题很简单:如果从未访问它进行编辑,为什么它会覆盖我的实体?我怎样才能避免覆盖?
谢谢大家
this.allSecurities = securities;
这意味着 allSecurities
和 securities
都引用相同的 ArrayList
。无论你对一个人做什么,都会发生在另一个人身上。您应该做的是复制 ArrayList
.
this.allSecurities = new ArrayList<>(securities);
现在对 securities
的任何修改都不会同时修改 allSecurities
。