【问题标题】:Android - How to filter a listview on multiple values?Android - 如何过滤多个值的列表视图?
【发布时间】:2013-09-10 15:03:41
【问题描述】:

我想根据歌曲名称和艺术家过滤列表视图。

目前我对歌曲标题有一个有效的过滤器。但我不知道如何同时过滤歌曲名称和艺术家。

这是我的活动:

listView.setTextFilterEnabled(true);
inputSongTitle.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
        adapterCorrectSong.getFilter().filter(cs);
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                  int arg3) {
    }

    @Override
    public void afterTextChanged(Editable s) {
    }
});

我在我的 ArrayAdapter 中使用此代码:

@Override
public Filter getFilter() {
    if (songFilter == null){
        songFilter  = new SongFilter();
    }
    return songFilter;
}

private class SongFilter extends Filter
{
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        constraint = constraint.toString().toLowerCase();
        FilterResults result = new FilterResults();
        if(constraint != null && constraint.toString().length() > 0)
        {
            ArrayList<CorrectSongResponse> filteredItems = new ArrayList<CorrectSongResponse>();

            for(int i = 0, l = allModelItemsArray.size(); i < l; i++)
            {
                CorrectSongResponse m = allModelItemsArray.get(i);
                if(m.getTitle().toLowerCase().contains(constraint.toString())) {
                    filteredItems.add(m);
                }
            }
            result.count = filteredItems.size();
            result.values = filteredItems;
        }
        else
        {
            synchronized(this)
            {
                result.values = allModelItemsArray;
                result.count = allModelItemsArray.size();
            }
        }
        return result;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {

        filteredModelItemsArray = (ArrayList<CorrectSongResponse>)results.values;
        notifyDataSetChanged();
        clear();
        for(int i = 0, l = filteredModelItemsArray.size(); i < l; i++)
            add(filteredModelItemsArray.get(i));
        notifyDataSetInvalidated();
    }
}

我现在想向“inputSongArtist”添加一个 addTextChangedListener。如何同时过滤标题和艺术家?

【问题讨论】:

    标签: android android-listview filter android-arrayadapter


    【解决方案1】:

    只需在您的自定义适配器中添加以下方法。

    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        arrData.clear();
        if (charText.length() == 0) {
            arrData.addAll(arrDataFilter);
        } else {
            for (ContactMyDataClass cMDC : arrDataFilter) {
                if(cMDC.getContactName().toLowerCase(Locale.getDefault()).contains(charText)){
                    arrData.add(cMDC);
                }
            }
        }
        notifyDataSetChanged();
    }
    

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        adapterContactList.filter(s.toString());
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-17
      • 1970-01-01
      • 1970-01-01
      • 2016-05-26
      • 2022-11-29
      • 1970-01-01
      相关资源
      最近更新 更多