Android:使用自定义适配器设置AutoCompleteTextView

时间:2013-08-31 14:18:20

标签: android listview adapter baseadapter autocompletetextview

我正在尝试将此适配器类与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();
        }
    }

}

}

1 个答案:

答案 0 :(得分:5)

确保您的适配器实现Filterable接口。

public class CustomCardListAdapter extends BaseAdapter implements Filterable