Android:使用自定义适配器设置AutoCompleteTextView
我正在尝试将此适配器类与AutoCompleteTextView一起使用。这个适配器是我在ListView中使用的适配器。查看文档,该类需要扩展ListAdapter和Filterable。BaseAdapter似乎扩展了ListAdapter,我的类实现了Filterable。然而,当我试图将它设置为我的ACTV适配器时,我得到了以下错误,我似乎无法理解它Android:使用自定义适配器设置AutoCompleteTextView,android,listview,adapter,baseadapter,autocompletetextview,Android,Listview,Adapter,Baseadapter,Autocompletetextview,我正在尝试将此适配器类与AutoCompleteTextView一起使用。这个适配器是我在ListView中使用的适配器。查看文档,该类需要扩展ListAdapter和Filterable。BaseAdapter似乎扩展了ListAdapter,我的类实现了Filterable。然而,当我试图将它设置为我的ACTV适配器时,我得到了以下错误,我似乎无法理解它 Bound mismatch: The generic method setAdapter(T) of type AutoComplete
Bound mismatch: The generic method setAdapter(T) of type AutoCompleteTextView is not applicable for the arguments (CustomCardListAdapter). The inferred type CustomCardListAdapter is not a valid substitute for the bounded parameter <T extends ListAdapter & Filterable>
绑定不匹配:AutoCompleteTextView类型的泛型方法setAdapter(T)不适用于参数(CustomCardListAdapter)。推断的类型CustomCardListAdapter不是绑定参数的有效替代品
我真的不知道这里少了什么。代码如下:
public class CustomCardListAdapter extends BaseAdapter {
// Main data structure
private ArrayList<NRCard> cards;
private ArrayList<NRCard> cardsBackup = null;
private Context ctx;
private CustomCardListAdapterFilter adapterFilter;
public CustomCardListAdapter(ArrayList<NRCard> cards, Context ctx) {
this.cards = cards;
this.cardsBackup = cards;
this.ctx = ctx;
}
@Override
public int getCount() {
return cards.size();
}
@Override
public Object getItem(int pos) {
return cards.get(pos);
}
@Override
public long getItemId(int pos) {
return pos;
}
/**
* Removes all NRCards where the side is the specified parameter
* @param side Side criteria
*/
public void removeSide(Side side) {
cards = (ArrayList<NRCard>)cardsBackup.clone();
for (ListIterator<NRCard> iter = cards.listIterator(cards.size()); iter.hasPrevious();) {
if (iter.previous().getSide() == side)
iter.remove();
}
notifyDataSetChanged();
}
public void restoreAllCards() {
cards = (ArrayList<NRCard>)cardsBackup.clone();
notifyDataSetChanged();
}
@Override
public View getView(int pos, View view, ViewGroup vg) {
View v = view;
if (v == null) {
LayoutInflater vi = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.card_search_view, null);
}
TextView cardNameView = (TextView) v
.findViewById(R.id.tv_searchView_cardName);
TextView cardIconView = (TextView) v
.findViewById(R.id.tv_searchView_cardIcon);
TextView cardDescView = (TextView) v
.findViewById(R.id.tv_searchView_cardDesc);
TextView cardEffectView = (TextView) v
.findViewById(R.id.tv_searchView_effect);
final NRCard card = cards.get(pos);
// Set card icon
String firstLetter = String.valueOf(card.getFaction().charAt(0));
cardIconView.setText(firstLetter);
cardIconView.setBackgroundColor(Utilities.getFactionColor(card.getSide(),
card.getFaction()));
// Set card main text
cardNameView.setText(card.getTitle());
// Set the card description
cardDescView.setText(card.getSideString() + " - " + card.getType()
+ " - " + String.valueOf(card.getCost()) + " credits");
// Set the card effect
String cardText = card.getText();
if (cardText.length() > 100) {
cardText = cardText.substring(0, 100) + "...";
}
cardEffectView.setText(Html.fromHtml(cardText));
return v;
}
}
public Filter getFilter() {
if (adapterFilter == null)
adapterFilter = new CustomCardListAdapterFilter();
return adapterFilter;
}
// Class enabling the filtering of this adapter
private class CustomCardListAdapterFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint == null || constraint.length() == 0) {
results.values = cardsBackup;
results.count = cardsBackup.size();
} else {
ArrayList<NRCard> filteredCardList = new ArrayList<NRCard>();
for (NRCard card : cardsBackup) {
if (card.getTitle()
.toLowerCase(Locale.getDefault())
.startsWith(
constraint.toString().toLowerCase(
Locale.getDefault()))) {
filteredCardList.add(card);
}
}
results.values = filteredCardList;
results.count = filteredCardList.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
if (results.count == 0)
notifyDataSetInvalidated();
else {
cards = (ArrayList<NRCard>) results.values;
notifyDataSetChanged();
}
}
}
公共类CustomCardListAdapter扩展了BaseAdapter{
//主要数据结构
私人ArrayList卡;
private ArrayList cardsBackup=null;
私有上下文ctx;
私人客户卡列表适配器过滤器适配器过滤器;
公共CustomCardListAdapter(ArrayList卡,上下文ctx){
这个。卡片=卡片;
this.cardsBackup=卡片;
this.ctx=ctx;
}
@凌驾
public int getCount(){
返回卡。大小();
}
@凌驾
公共对象getItem(int-pos){
返回卡。获取(pos);
}
@凌驾
公共长getItemId(int-pos){
返回pos;
}
/**
*移除侧面为指定参数的所有NRC卡
*@param-side标准
*/
公共空间移除(侧边){
cards=(ArrayList)cardsBackup.clone();
for(ListIterator iter=cards.ListIterator(cards.size());iter.hasPrevious();){
if(iter.previous().getSide()==side)
iter.remove();
}
notifyDataSetChanged();
}
公共空间恢复记录(){
cards=(ArrayList)cardsBackup.clone();
notifyDataSetChanged();
}
@凌驾
公共视图getView(内部位置、视图视图、视图组vg){
视图v=视图;
如果(v==null){
LayoutInflater vi=(LayoutInflater)ctx
.getSystemService(上下文布局\充气机\服务);
v=vi.充气(R.布局.卡片搜索视图,空);
}
TextView卡名视图=(TextView)v
.findviewbyd(R.id.tv\u searchView\u cardName);
TextView CardConView=(TextView)v
.findViewById(R.id.tv\u searchView\u cardIcon);
TextView cardDescView=(TextView)v
.findViewById(R.id.tv\u searchView\u cardDesc);
TextView cardEffectView=(TextView)v
.findviewbyd(R.id.tv\u searchView\u效果);
最终NRCard卡=卡。获取(pos);
//设置卡片图标
String firstLetter=String.valueOf(card.get派系().charAt(0));
cardIconView.setText(第一个字母);
CardConView.setBackgroundColor(Utilities.getColor(card.getSide()),
card.get派系());
//设置卡主文本
cardNameView.setText(card.getTitle());
//设置卡片描述
cardDescView.setText(card.getSideString()+“-”+card.getType()
+“-”+String.valueOf(card.getCost())+“credits”);
//设置卡片效果
String cardText=card.getText();
如果(cardText.length()>100){
cardText=cardText.substring(0100)+“…”;
}
cardfectview.setText(Html.fromHtml(cardText));
返回v;
}
}
公共过滤器getFilter(){
if(适配器过滤器==null)
adapterFilter=新的CustomCardListAdapterFilter();
回流适配器过滤器;
}
//类启用此适配器的筛选
私有类CustomCardListAdapterFilter扩展筛选器{
@凌驾
受保护的筛选器结果性能筛选(CharSequence约束){
FilterResults results=新的FilterResults();
if(constraint==null | | constraint.length()==0){
results.values=cardsBackup;
results.count=cardsBackup.size();
}否则{
ArrayList filteredCardList=新的ArrayList();
用于(NRCard卡:cardsBackup){
if(card.getTitle()
.toLowerCase(Locale.getDefault())
.startsWith(
constraint.toString().toLowerCase(
Locale.getDefault()){
filteredCardList.add(卡片);
}
}
results.values=filteredCardList;
results.count=filteredCardList.size();
}
返回结果;
}
@凌驾
受保护的无效发布结果(CharSequence约束,
FilterResults(结果){
如果(results.count==0)
notifyDataSetionValidated();
否则{
cards=(ArrayList)results.values;
notifyDataSetChanged();
}
}
}
}确保适配器实现了
可过滤接口
public class CustomCardListAdapter extends BaseAdapter implements Filterable
你能帮我做这个吗