如何显示“无结果”在可过滤的 ListView 中?

发布于 2024-11-10 10:46:18 字数 1371 浏览 4 评论 0原文

我有一个 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());    

    }
};

条件:

  1. 我在 ListView 中有 10 个项目。

问题:

  1. 当我第一次输入第一个字符时,为什么 watcherAdapter.getCount()ListView 中返回 10 (作为初始值)而不是返回的过滤结果计数? watcherAdapter.getCount() 似乎在 ListView 中显示结果时单击了一下。
  2. 当我在 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:

  1. I have 10 items in ListView.

Question:

  1. When I first type the first character, why the watcherAdapter.getCount() returns 10 (as initial) in ListView instead of the returned filter result count? The watcherAdapter.getCount() seems a-click late for the displayed result in ListView.
  2. How I achieve to show "No Result" in ListView when there is no match results as I type on the EditText?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

も星光 2024-11-17 10:46:18

将 TextView 添加到列表上方或下方的布局中会更清晰,您可以在其中显示消息:

<TextView 
android:id="@+id/noResultsFoundView" 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="No result"
android:visibility="gone"
/>

当您有结果时,将可见性设置为“消失”。如果没有结果,请将其设置为“可见”。

要侦听此情况,您可以实现自定义过滤器或至少重写

publishResults(CharSequence constraint, FilterResults results)

Filter 中的方法,其中正确的更新计数将在 FilterResults 参数中传递。

在publishResults中,您调用活动中的方法来更新noResultsFoundView的可见性。您访问活动的方式取决于您的过滤器所在的位置。如果它位于活动的内部类中,那么这很容易。否则,您可以将活动作为参数传递以实例化适配器,并将其存储为实例变量。

It's cleaner to add a TextView to your layout, above or bellow the list, where you show the message:

<TextView 
android:id="@+id/noResultsFoundView" 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="No result"
android:visibility="gone"
/>

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

publishResults(CharSequence constraint, FilterResults results)

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.

夜巴黎 2024-11-17 10:46:18

当然,可以在可过滤列表中不显示任何结果。请参考以下代码

 @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.home, menu);

        MenuItem searchItem = menu.findItem(R.id.menu_search);
        final AppCompatEditText searchView = (AppCompatEditText) MenuItemCompat.getActionView(searchItem);
        if (searchView != null) {
            searchView.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                          int arg3) {
                    if(mAdapter==null)
                        return;
                        mAdapter.getFilter().filter(arg0.toString());
                        if(mAdapter.getItemCount()<1){
                            listView.setVisibility(View.GONE);
                            txtEmptyList.setVisibility(View.VISIBLE);
                        }else{
                            listView.setVisibility(View.VISIBLE);
                            txtEmptyList.setVisibility(View.GONE);
                        }
                }

                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1,
                                              int arg2, int arg3) {
                }

                @Override
                public void afterTextChanged(Editable arg0) {

                }
            });
        }
        MenuItemCompat.setOnActionExpandListener(searchItem, this);
        MenuItemCompat.setActionView(searchItem, searchView);
        super.onCreateOptionsMenu(menu, inflater);
    }

Of course it is possible to show no result in filterable list. Please refere the below code

 @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.home, menu);

        MenuItem searchItem = menu.findItem(R.id.menu_search);
        final AppCompatEditText searchView = (AppCompatEditText) MenuItemCompat.getActionView(searchItem);
        if (searchView != null) {
            searchView.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                          int arg3) {
                    if(mAdapter==null)
                        return;
                        mAdapter.getFilter().filter(arg0.toString());
                        if(mAdapter.getItemCount()<1){
                            listView.setVisibility(View.GONE);
                            txtEmptyList.setVisibility(View.VISIBLE);
                        }else{
                            listView.setVisibility(View.VISIBLE);
                            txtEmptyList.setVisibility(View.GONE);
                        }
                }

                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1,
                                              int arg2, int arg3) {
                }

                @Override
                public void afterTextChanged(Editable arg0) {

                }
            });
        }
        MenuItemCompat.setOnActionExpandListener(searchItem, this);
        MenuItemCompat.setActionView(searchItem, searchView);
        super.onCreateOptionsMenu(menu, inflater);
    }
清引 2024-11-17 10:46:18
if(!fillMaps.isEmpty())
            {
            SimpleAdapter adapter = new SimpleAdapter(
                    WorldClockActivity.this, fillMaps, R.layout.grid_item,
                    from, to);
            lv1.setAdapter(adapter);
            }
            else
            {      String[] emptyList = new String[] {"No record found"};
            lv1.setAdapter(new ArrayAdapter<String>(WorldClockActivity.this,R.layout.list_item, emptyList));
            }

您应该在上面的代码中使用两个适配器来执行此操作,我使用了两个适配器,第一个适配器是当我们在地图中找到项目时设置的,如果地图为空,那么您应该添加另一个适配器,我已将其置于其他条件下。我已经完成了并且运行良好。
我希望你能得到你的解决方案。

if(!fillMaps.isEmpty())
            {
            SimpleAdapter adapter = new SimpleAdapter(
                    WorldClockActivity.this, fillMaps, R.layout.grid_item,
                    from, to);
            lv1.setAdapter(adapter);
            }
            else
            {      String[] emptyList = new String[] {"No record found"};
            lv1.setAdapter(new ArrayAdapter<String>(WorldClockActivity.this,R.layout.list_item, emptyList));
            }

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.

浮光之海 2024-11-17 10:46:18

我最后使用的唯一解决方案是执行自定义搜索过滤器......拆分单词,将它们标记化,如果匹配,则将其作为列表的适配器放在数组中。它按照我想要的方式工作

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文