Android - How to filter a listview on multiple values?

Deadly 提交于 2019-12-11 09:37:07

问题


I want to filter a listView on song title and artist.

Currently I have a working filter on song title. But I can't figure out how to filter on song title and artist at the same moment.

This is my activity:

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

I use this code in my 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();
    }
}

I now want to add an addTextChangedListener to "inputSongArtist". How can I filter both on Title and Artist at the same time?


回答1:


Just add the below method in your custom adapter.

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

and than

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


来源:https://stackoverflow.com/questions/18722444/android-how-to-filter-a-listview-on-multiple-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!