1
我有我的AutoCompleteTextView問題,的Android AutoCompleteTextView顯示已選定一個
當我選擇的建議之一,
它應該顯示的產品名稱(垃圾食物或西餐)。 任何人都可以幫我解決這個問題。以下是適配器&過濾器類。
篩選類 公共類產品篩選延伸濾波器{ 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;
}
}
非常感謝你!我的問題解決了。 – user2412351
我反對一個新問題,它返回建議項目的位置,而不是正確的ID。假設它返回5條建議,我選擇了第一條,但它會在字段中顯示一個數組列表中的第一個,而不是我選擇的那個。你明白嗎? – user2412351
使用'getItemAtPosition()'方法。代碼: - autoComplete.setOnItemClickListener(新AdapterView.OnItemClickListener(){ @Override 公共無效onItemClick(適配器視圖>適配器視圖,視圖視圖,INT I,長L){ selectedProduct = adapterView.getItemAtPosition(I); } } ); –