该列表不对原始数据执行过滤

时间:2015-12-29 12:32:06

标签: android android-listview android-adapter listadapter

我已经实现了可过滤接口。 我的适配器代码

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

1 个答案:

答案 0 :(得分:0)

在我的情况下工作正常

private class ValueFilter extends Filter
{

    //Invoked in a worker thread to filter the data according to the constraint.
    @Override
    protected FilterResults performFiltering(CharSequence constraint)
    {
        FilterResults results;
        try
        {
            nearbyList = FilterList;
            results = new FilterResults();
            if (constraint != null && constraint.length() > 0)
            {
                ArrayList<Card> filterList = new ArrayList<Card>();
                String SearchText = constraint.toString().toUpperCase();
                for (int i = 0; i < nearbyList.size(); i++)
                {
                    if ((nearbyList.get(i).name.toUpperCase()).contains(SearchText))// || (nearbyList.get(i).last_name.toUpperCase()).contains(SearchText) || (nearbyList.get(i).company_name.toUpperCase()).contains(SearchText))
                    {
                        Card card = new Card();

                        card.user_id = (nearbyList.get(i).user_id);

                        card.name = (nearbyList.get(i).name);
                        card.status = (nearbyList.get(i).status);
                        card.profile_image = (nearbyList.get(i).profile_image);
                        card.company_name = (nearbyList.get(i).company_name);
                        card.designation = (nearbyList.get(i).designation);

                        filterList.add(card);
                    }
                }
                results.count = filterList.size();
                results.values = filterList;
            }
            else
            {
                results.count = nearbyList.size();
                results.values = nearbyList;
            }
            return results;
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    //Invoked in the UI thread to publish the filtering results in the user interface.
    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results)
    {
        try
        {
            nearbyList = (ArrayList<Card>) results.values;
            notifyDataSetChanged();
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

nearbyList = FilterList;此行用于过滤器的主适配器数据,因此每次都可以通过传递主适配器数据。