0
我實施了可過濾界面。 代碼爲我的適配器該列表未對原始數據執行過濾器
public class LyricListAdapter extends BaseAdapter implements Filterable {
Context context;
List<Lyric> lyrics;
LyricsFilter filter;
public LyricListAdapter(Context context, List<Lyric> lyrics) {
this.context = context;
this.lyrics = lyrics;
getFilter();
}
@Override
public int getCount() {
return lyrics.size();
}
@Override
public Object getItem(int position) {
return lyrics.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
//View holder
class ViewHolder {
ImageView imgThumb;
TextView lblTitle;
TextView lblArtist;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if(convertView == null) {
convertView = inflater.inflate(R.layout.row_lyric, null);
holder = new ViewHolder();
holder.imgThumb = (ImageView) convertView.findViewById(R.id.imgThumb);
holder.lblTitle = (TextView) convertView.findViewById(R.id.lblTitle);
holder.lblArtist = (TextView) convertView.findViewById(R.id.lblArtist);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
try {
Lyric lyric = (Lyric) getItem(position);
holder.imgThumb.setImageResource(lyric.getThumb());
holder.lblTitle.setText(lyric.getLyricTitle());
holder.lblArtist.setText(lyric.getLyricArtist());
} catch (Exception ex){
ex.printStackTrace();
}
return convertView;
}
@Override
public Filter getFilter() {
if(filter == null){
filter = new LyricsFilter();
}
return filter;
}
private class LyricsFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if(constraint == null || constraint.length() == 0){
results.values = lyrics;
results.count = lyrics.size();
} else {
ArrayList<Lyric> filteredLyrics = new ArrayList<Lyric>();
for(Lyric l : lyrics){
Log.d("!!!!!!!!!!!", l.getLyricTitle());
if(l.getLyricTitle().toLowerCase().contains(constraint.toString().toLowerCase()))
filteredLyrics.add(l);
}
results.values = filteredLyrics;
results.count = filteredLyrics.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
lyrics = (ArrayList<Lyric>) results.values;
notifyDataSetChanged();
}
}
}
- 問題是,當我在編輯文本中的列表中得到 過濾器。
- 當我鍵入下一個字符時,它從篩選的 結果中篩選出的結果不是原始列表。
- ,甚至當我刪除編輯文本文本不顯示歌詞
的完整列表過濾通過的另一項活動叫這樣的:
public void onTextChanged(CharSequence s, int start, int before, int count) {
Toast.makeText(LyricsListActivity.this, s, Toast.LENGTH_SHORT).show();
Log.d("12123123", s.toString());
adapter.getFilter().filter(s);
}
看看[這裏](http://stackoverflow.com/questions/33023111/my-custom-list-filter-is-not-working) – Blackbelt