如何显示“无结果”在可过滤的 ListView 中?
我有一个 ListView
和一个 EditText
。我在 EditText
上实现 addTextChangedListener
来过滤 ListView
内容。
leftList.setTextFilterEnabled(true);
et_search.addTextChangedListener(filterTextWatcher);
然后 TextWatcher
是:
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (watcherAdapter==null) {
return;
}
watcherAdapter.getFilter().filter(s);
Log.e(TAG, "OnTextChange: " + s + " start: " + start +
" before: " + before + " count: " + count + " adapter: " +
watcherAdapter.getCount());
}
};
条件:
- 我在
ListView
中有 10 个项目。
问题:
- 当我第一次输入第一个字符时,为什么
watcherAdapter.getCount()
在ListVie
w 中返回10
(作为初始值)而不是返回的过滤结果计数?watcherAdapter.getCount(
) 似乎在ListView
中显示结果时单击了一下。 - 当我在
EditText
上输入时没有匹配结果时,如何在ListView
中显示“No Result”
?
I have a ListView
and an EditText
. I implement addTextChangedListener
on EditText
to filter the ListView
content.
leftList.setTextFilterEnabled(true);
et_search.addTextChangedListener(filterTextWatcher);
and then the TextWatcher
is:
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (watcherAdapter==null) {
return;
}
watcherAdapter.getFilter().filter(s);
Log.e(TAG, "OnTextChange: " + s + " start: " + start +
" before: " + before + " count: " + count + " adapter: " +
watcherAdapter.getCount());
}
};
Condition:
- I have 10 items in
ListView
.
Question:
- When I first type the first character, why the
watcherAdapter.getCount()
returns10
(as initial) inListVie
w instead of the returned filter result count? ThewatcherAdapter.getCount(
) seems a-click late for the displayed result inListView
. - How I achieve to show
"No Result"
inListView
when there is no match results as I type on theEditText
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将 TextView 添加到列表上方或下方的布局中会更清晰,您可以在其中显示消息:
当您有结果时,将可见性设置为“消失”。如果没有结果,请将其设置为“可见”。
要侦听此情况,您可以实现自定义过滤器或至少重写
Filter 中的方法,其中正确的更新计数将在 FilterResults 参数中传递。
在publishResults中,您调用活动中的方法来更新noResultsFoundView的可见性。您访问活动的方式取决于您的过滤器所在的位置。如果它位于活动的内部类中,那么这很容易。否则,您可以将活动作为参数传递以实例化适配器,并将其存储为实例变量。
It's cleaner to add a TextView to your layout, above or bellow the list, where you show the message:
When you have results, you set the visibility to GONE. When you have no results you set it to VISIBLE.
To listen for this you could implement a custom filter or at least override the method
from the Filter, where the correct, updated count is passed in FilterResults parameter.
In publishResults you call a method from the activity to update the visibility of noResultsFoundView. How you access the activity depends of where your filter is. If it's in an inner class of the activity then it's easy. Otherwise you for example pass the activity as parameter to instantiate the adapter, and store it as intance variable.
当然,可以在可过滤列表中不显示任何结果。请参考以下代码
Of course it is possible to show no result in filterable list. Please refere the below code
您应该在上面的代码中使用两个适配器来执行此操作,我使用了两个适配器,第一个适配器是当我们在地图中找到项目时设置的,如果地图为空,那么您应该添加另一个适配器,我已将其置于其他条件下。我已经完成了并且运行良好。
我希望你能得到你的解决方案。
You should use two adapter for doing this in the above code i have use two adapter first one is set when we found item in the map if map is empty then you should add another adapter i have put it in else condition.I have done it and it is running fine.
I hope you got your solution.
我最后使用的唯一解决方案是执行自定义搜索过滤器......拆分单词,将它们标记化,如果匹配,则将其作为列表的适配器放在数组中。它按照我想要的方式工作
The only solution I use at last is to do my custom search filter....Split the words, tokenizes them, and if matched put it on array as adapter to the List. And it works as I want it
比其他答案更简单、更清晰:看一下
ListActivity
的空View
:http://developer.android.com/reference/android/app/ListActivity.html
另请参阅:
如果筛选列表视图不包含任何内容,如何显示特殊消息还有结果吗?
Android:将空视图设置为列表view
当 ListView 为空时显示空视图
Simpler and cleaner that other answers: take a look at
ListActivity
's emptyView
:http://developer.android.com/reference/android/app/ListActivity.html
See also:
How to display a special message if a filtered list view contains no results any more?
Android: set empty view to a list view
Showing empty view when ListView is empty