我的AutoCompleteTextView有问题,
当我选择其中一个建议时,
应显示产品名称(垃圾食品或西餐)。 有人请帮我解决这个问题。以下是Adapter&过滤类。
过滤类 公共类ProductFilter扩展Filter { AdapterProductAutoComplete adapterProductAutoComplete; 列出originalList; 列出filteredList;
public ProductFilter (AdapterProductAutoComplete adapterProductAutoComplete, List<Product>
originalList){
super();
this.adapterProductAutoComplete = adapterProductAutoComplete;
this.originalList = originalList;
this.filteredList = new ArrayList<>();
}
@Override
protected Filter.FilterResults performFiltering (CharSequence constraint){
filteredList.clear();
final FilterResults results = new FilterResults();
if(constraint == null || constraint.length() == 0){
filteredList.addAll(originalList);
}else{
final String filterPattern = constraint.toString().toLowerCase().trim();
for (final Product product : originalList){
if(product.getProductName().toLowerCase().contains(filterPattern) || Integer
.toString(product.getProductId()).toLowerCase().contains(filterPattern)){
filteredList.add(product);
}
}
}
results.values = filteredList;
results.count = filteredList.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
adapterProductAutoComplete.filteredProducts.clear();
adapterProductAutoComplete.filteredProducts.addAll((List) results.values);
adapterProductAutoComplete.notifyDataSetChanged();
}
}
适配器类
public class AdapterProductAutoComplete extends ArrayAdapter<Product>{
private final List<Product> products;
public List<Product> filteredProducts = new ArrayList<>();
public AdapterProductAutoComplete(Context context, List<Product> products){
super(context, 0, products);
this.products = products;
}
@Override
public int getCount(){
return filteredProducts.size();
}
@Override
public Filter getFilter(){
return new ProductFilter(this, products);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
Product product = filteredProducts.get(position);
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.list_row_actproduct, parent, false);
TextView tvCode = (TextView) convertView.findViewById(R.id.actproduct_productcode);
TextView tvName = (TextView) convertView.findViewById(R.id.actproduct_productname);
tvCode.setText(Integer.toString(product.getProductId()));
tvName.setText(product.getProductName());
return convertView;
}
}
答案 0 :(得分:1)
覆盖toString()
课程中的Product
方法,因为AutoCompleteTextView
取自toString()
的值。将此代码添加到Product
类: -
@Override
public String toString(){
return getProductName();
}
Object类中toString()方法的默认实现是: -
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
这是你现在得到的。