Android AutoCompleteTextView 显示选中的

标签 android autocompletetextview

我的 AutoCompleteTextView 有问题,

enter image description here

当我选择其中一个建议时,

enter image description here

它应该显示产品名称(垃圾食品或西餐)。 任何人都请帮我解决这个问题。下面是适配器和过滤器类。

过滤器类 公共(public)类 ProductFilter 扩展过滤器 { 适配器产品自动完成适配器产品自动完成; 列出原始列表; 列表过滤列表;

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

最佳答案

重写 Product 类中的 toString() 方法,因为 AutoCompleteTextViewtoString() 获取值。将此代码添加到您的 Product 类:-

@Override
public String toString(){
    return getProductName();
}

Object 类中 toString() 方法的默认实现是这样的:-

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

这就是你现在得到的。

关于Android AutoCompleteTextView 显示选中的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41135294/

相关文章:

android - WebView 线程永不停止(WebViewCoreThread、CookieSyncManager、http[0-3])

android - react-native 中的异步通知

android - 应用程序在菜单中不可见

android - 如何禁用 Android AutoCompleteTextView 的拼写检查器?

android - 没有字典提示的 AutoCompleteTextView

android - 在操作栏中实现 SearchView

java - 使用双数组的最佳实践

android - Android 中的 Tesseract 字符识别问题(但 iOS 上没有?)

android - autocompletetextview 不建议我想要什么

java - 在自动完成 TextView 中获取后退空格按钮单击操作事件