【问题标题】:android - listview filter countandroid - listview 过滤器计数
【发布时间】:2013-01-15 17:05:18
【问题描述】:

所以在我在列表视图上设置过滤器后:

//Log adapter count before filter
listView.getFilter().filter(searchStr)
//Log adapter count after filter

我想要实现的是获取该过滤结果的计数。就像之前有 10 个项目,然后我应用过滤器,所以现在只会出现 5 个项目,我想得到那个计数“5”。我尝试在过滤器之前和之后检查适配器计数,但没有成功。如果我应用过滤器,它们将显示相同的计数(我正在使用 BaseExpandableListAdapter),如果我再次应用过滤器,数字会从之前发生变化(但过滤器的前后仍然相同)。

以下是我在日志中得到的示例结果:

Before filter the count is 10. After filter the count is 10.
Before filter the count is 8.  After filter the count is 8.

我想也许我的适配器没有立即获得反射计数,但是在第二个过滤器上,它会更改值,所以我认为过滤器之后的 notifyDataSetChanged 会有所不同,但事实并非如此。任何帮助将不胜感激。

谢谢。

【问题讨论】:

    标签: android filter android-listview filtering android-adapter


    【解决方案1】:

    这就是我设法做到的:

    myAdapter.getFilter().filter(searchText, new Filter.FilterListener() {
        public void onFilterComplete(int count) {
             Log.d("log", "result count:" + count);
        }
    });
    

    【讨论】:

    • 这实际上解决了我最初的问题,加上另一个问题:我的列表出现了所有元素的一小部分时间,因为过滤器透明地在 Asynctask 中运行,并且 setAdapter 被调用在 publishResults 实际发生之前
    • 缺失);在您的代码中,它在过滤器附近打开(但没有右括号。所以添加);最后
    【解决方案2】:

    在应用过滤器后尝试调用listview.getCount()

    【讨论】:

    • 我正在使用 ExpandableListView。并使用它返回组计数而不是每个组的子计数。
    猜你喜欢
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    • 2012-12-11
    • 2013-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多