我的listview有一个过滤器,它的工作方式是我想要的一半。
例如,我的数据库是:
早上好,晚安
如果我输入Good,则显示--->早上好;晚安
如果我输入,好的m,显示--->早上好
如果我删除“m”,Good,则显示--->早上好
如果我添加“n”Good n ---->这是空白的
所以我希望刷新每个字母的listview。我不太确定问题是在执行过滤或发布结果上还是在TextChangedListener上。我用google搜索它并找到了一些例子,但它们没有用。提前致谢。
在执行过滤时,我几乎解决了这个问题:
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
// implement here the filter logic
if (constraint != null && constraint.length() != 0 && constraint.length()*vocabLista.size() != 0)
// constraint.length()*vocabLista.size() != 0 for every time that haven't showed results
{
// perform filtering operation
这是我的代码:
TextChangedListener:
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
System.out.println("Text [" + s + "]");
j_adapter.getFilter().filter(s.toString());
j_adapter.notifyDataSetChanged();
}
@Override
public void afterTextChanged(Editable s) {
}
});
过滤
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
// We implement here the filter logic
if (constraint != null && constraint.length() != 0)
// && constraint.length()*vocabLista.size() != 0
{
// perform filtering operation
List<Vocabulario> nVocabLista = new ArrayList<Vocabulario>();
for (Vocabulario p : vocabLista) {
if (p.getEnglish().toUpperCase().startsWith(constraint.toString().toUpperCase()) | p.getPortugues().toUpperCase().startsWith(constraint.toString().toUpperCase()) | p.getPortugues().toUpperCase().contains(constraint.toString().toUpperCase()) | p.getEnglish().toUpperCase().contains(constraint.toString().toUpperCase()))
{
nVocabLista.add(p);
}
}
results.values = nVocabLista;
results.count = nVocabLista.size();
} else
{
{
// No filter implemented return all the list
results.values = origVocabLista;
results.count = origVocabLista.size();
}
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,FilterResults results)
{
// inform the adapter about the new list filtered
vocabLista = (List<Vocabulario>) results.values;
if (results.count != 0)
{
results.values = vocabLista;
results.count = vocabLista.size();
notifyDataSetChanged();
}
else
{
notifyDataSetInvalidated();
}
}
}