Android的 - 如何过滤多个值列表视图? [英] Android - How to filter a listview on multiple values?
本文介绍了Android的 - 如何过滤多个值列表视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要过滤的歌曲名称和艺术家一个ListView。
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.
这是我的活动:
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) {
}
});
我用这个code在我ArrayAdapter:
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();
}
}
我现在要添加addTextChangedListener为inputSongArtist。我怎么能同时过滤无论在标题和艺术家?
I now want to add an addTextChangedListener to "inputSongArtist". How can I filter both on Title and Artist at the same time?
推荐答案
只需添加在您的自定义适配器下面的方法。
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();
}
和比
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapterContactList.filter(s.toString());
}
这篇关于Android的 - 如何过滤多个值列表视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文