gpt4 book ai didi

android - 自定义 AutoCompleteTextView Android

转载 作者:行者123 更新时间:2023-11-30 02:39:52 25 4
gpt4 key购买 nike

我想在 Android 中自定义 AutoCompleteTextView。通常只有当我们开始在 EditText 中输入文本时,下拉菜单才会弹出。但是我想在单击 AutoCompleteTextView 时显示所有元素,然后在我开始键入文本时显示过滤后的元素。为了实现这一点,我应该实现什么方法。

最佳答案

这里的代码对我有用,

将此适配器设置为 autocompletetextview

AutoCompleteTextView etProductSearch = (AutoCompleteTextView)getView().findViewById(R.id.edtSearchBoxTakeOrder);
ProductSearchAdapter adapter = new ProductSearchAdapter(getActivity(), android.R.layout.simple_dropdown_item_1line, productList);
etProductSearch.setAdapter(adapter );

ProductSearchAdapter 类

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.TextView;

public class ProductSearchAdapter extends ArrayAdapter<ProductDataModel> {
private ArrayList<ProductDataModel> items;
private ArrayList<ProductDataModel> itemsAll;
private ArrayList<ProductDataModel> suggestions;
private int viewResourceId;

@SuppressWarnings("unchecked")
public ProductSearchAdapter(Context context, int viewResourceId,
ArrayList<ProductDataModel> items) {
super(context, viewResourceId, items);
this.items = items;
this.itemsAll = (ArrayList<ProductDataModel>) items.clone();
this.suggestions = new ArrayList<ProductDataModel>();
this.viewResourceId = viewResourceId;
}

public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(viewResourceId, null);
}
ProductDataModel product = items.get(position);
if (product != null) {
TextView productLabel = (TextView) v.findViewById(android.R.id.text1);
if (productLabel != null) {
productLabel.setText(product.getProductName());
}
}
return v;
}

@Override
public Filter getFilter() {
return nameFilter;
}

Filter nameFilter = new Filter() {
public String convertResultToString(Object resultValue) {
String str = ((ProductDataModel) (resultValue)).getProductName();
return str;
}

@Override
protected FilterResults performFiltering(CharSequence constraint) {
if (constraint != null) {
suggestions.clear();
for (ProductDataModel product : itemsAll) {
if (product.getProductName().toLowerCase()
.startsWith(constraint.toString().toLowerCase())) {
suggestions.add(product);
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = suggestions;
filterResults.count = suggestions.size();
return filterResults;
} else {
return new FilterResults();
}
}

@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
@SuppressWarnings("unchecked")
ArrayList<ProductDataModel> filteredList = (ArrayList<ProductDataModel>) results.values;
if (results != null && results.count > 0) {
clear();
for (ProductDataModel c : filteredList) {
add(c);
}
notifyDataSetChanged();
}
}
};

}

关于android - 自定义 AutoCompleteTextView Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25943140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com