2013-08-31 173 views
2

我正在嘗試將此適配器類與AutoCompleteTextView一起使用。這個適配器是我在ListView中使用的適配器。查看文檔,該類需要擴展ListAdapter和Filterable。 BaseAdapter似乎擴展了ListAdapter,而我的類實現了Filterable。但是,當我嘗試將其設置爲我的ACTV適配器時,出現以下錯誤,我似乎無法理解它;Android:使用自定義適配器設置AutoCompleteTextView

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(); 
     } 
    } 

} 

}

+0

你可以幫我這個http://stackoverflow.com/questions/29046302/how-to-implement-autocompletetextview-with-listview – 2015-03-16 06:19:25

回答

5

確保您的適配器實現Filterable接口。

public class CustomCardListAdapter extends BaseAdapter implements Filterable 
+0

沒錯。那就是訣竅。謝謝! – slaughterize

+0

你能幫我這個http://stackoverflow.com/questions/29046302/how-to-implement-autocompletetextview-with-listview – 2015-03-16 06:19:29

相關問題