我正在尝试将此适配器类与 AutoCompleteTextView 一起使用。这个适配器是我在 ListView 中使用的。查看文档,该类需要扩展 ListAdapter 和 Filterable。BaseAdapter 似乎扩展了 ListAdapter,而我的类实现了 Filterable。但是,当我尝试将其设置为我的 ACTV 适配器时,出现以下错误,我似乎无法理解它;
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>
我真的不知道这里缺少什么。这是代码:
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();
}
}
}
}