gpt4 book ai didi

android - 如何将自定义适配器添加到 AutoCompleteTextView

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:51:21 26 4
gpt4 key购买 nike

有什么简单的方法可以将 2 TextView 下拉列表设置为 AutoCompleteTextView。

android.R.layout.two_line_list_item 我找不到任何示例如何使用。

所以,我尝试了这个:

public class TwoLineDropdownAdapter extends BaseAdapter {

private LayoutInflater mInflater = null;
private Activity activity;
public ArrayList<TwoLineDropDown> values = new ArrayList<TwoLineDropDown>();

public TwoLineDropdownAdapter(Activity a, ArrayList<TwoLineDropDown> items) {

values = items;
activity = a;
mInflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount() {

return values.size();
}

public TwoLineDropDown getItem(int position) {

return values.get(position);
}

public long getItemId(int position) {

return position;
}

public static class ViewHolder {

public TextView title;
public TextView description;
}

public View getView(final int position, View convertView, ViewGroup parent) {

ViewHolder holder;

if (convertView == null) {

holder = new ViewHolder();

convertView = mInflater.inflate(R.layout.dropdown_text_twoline,
parent, false);
holder.title = (TextView) convertView
.findViewById(R.id.text1);
holder.description = (TextView) convertView
.findViewById(R.id.text2);

convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

return convertView;
}

public void add(TwoLineDropDown ei) {

values.add(ei);
}
}

但我在这里遇到一个问题:

TwoLineDropdownAdapter AutoCompleteAdapter = new TwoLineDropdownAdapter(this, items);
myAutoComplete.setAdapter(AutoCompleteAdapter);

设置适配器时显示:

Bound mismatch: The generic method setAdapter(T) of type AutoCompleteTextView is not applicable for the arguments (TwoLineDropdownAdapter). The inferred type TwoLineDropdownAdapter is not a valid substitute for the bounded parameter

如何解决?

谢谢

最佳答案

这里的代码对我有用,

将此适配器设置为 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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16962340/

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