2016-12-14 4 views
1

私は私のAutoCompleteTextViewに問題がある、それは製品名が表示されるはずですアンドロイドAutoCompleteTextViewが選びだし表示1

enter image description here

enter image description here

私は提案のいずれかを選択

、(ジャンク食品または西洋食品)。 誰でもこの問題を解決するのを手伝ってください。以下はアダプタ&フィルタクラスです。

フィルタクラス パブリッククラスプロダクトフィルタは、フィルタ{ AdapterProductAutoComplete adapterProductAutoCompleteを拡張します。 リスト元リスト; フィルタリングされたリストをリストします。

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

あなたProductクラスのアダプタクラス

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

答えて

1

オーバーライドtoString()方法AutoCompleteTextViewtoString()から値を取るので。あなたのProductクラスにこのコードを追加します -

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

のtoStringのデフォルトの実装()Objectクラスのメソッドはこれです: -

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

それはあなたが今になっているものです。

+0

ありがとうございます!私の問題は解決する。 – user2412351

+0

私は新しい問題に対抗し、正しいIDの代わりに提案項目の位置を返します。それは5提案を返すと言う、私は最初の1つを選択しますが、それはarraylistのフィールドで、私が選択しているものではなく、表示されます。わかりますか? – user2412351

+0

は 'getItemAtPosition()'メソッドを使います。コード: - autoComplete.setOnItemClickListener(新しいAdapterView.OnItemClickListener(){ @Override公共ボイドonItemClick(AdapterView adapterView、ビュービュー、int型I、長いL){ selectedProduct = adapterView.getItemAtPosition(I); }} ); –

関連する問題