【问题标题】:Hide ListView while filtering if result is empty listView如果结果为空,则在过滤时隐藏 ListView
【发布时间】:2015-05-30 02:35:06
【问题描述】:

我遇到了一个问题。

我想在列表视图为空时隐藏它。 我可以成功过滤并获得结果,但是当它没有结果时,我想隐藏列表视图。

filterEditText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // Call back the Adapter with current character to Filter


            String searchedquery = s.toString().replaceAll(" ","");
            //The line below searches the listview and shows the results

           adapter1.getFilter().filter(searchedquery);
           int i= list.getAdapter().getCount();
            String i2=String.valueOf(i);
            Log.d("number of membres in list view are  ::::::::::::",i2);
            System.out.println(i);
            if (i2.equals("0")){
                list.setVisibility(View.GONE);
            }
            else {

                list.setVisibility(View.VISIBLE);

            } });

谁能帮帮我! ? 提前非常感谢....

【问题讨论】:

  • 当数组适配器的结果为空时,我必须隐藏列表视图......并且无法获得如何做到这一点@MeshBoy
  • 如果您的 i=0 发生,您是否调试过?
  • 它是 0 但在我输入第二个字母之后 ....
  • 我只是建议您在适配器 1 中过滤查询结果,但不会刷新您的列表和检查列表适配器的计数。所以最好检查一下adapter1对象的计数并用它来隐藏/显示。

标签: android android-listview filtering android-arrayadapter


【解决方案1】:
String searchedquery = s.toString().replaceAll(" ","");

if(searchedquery.length>0){
            //The line below searches the listview and shows the results
           adapter1.getFilter().filter(searchedquery);
           adapter1.notifyDataSetChanged();

            if (adapter1.getCount()>0)
                list.setVisibility(View.VISIBLE);
            else 
                list.setVisibility(View.INVISIBLE);
     }

【讨论】:

  • adapter1.notifyDataSetChanged();之前的条件。检查我的答案。
  • 谢谢它的工作,但有一个问题需要写两个字母它确实更新第二个字母而不是第一个字母
  • 使用 s.toString().trim();不是 s.toString().replaceAll(" ","");
【解决方案2】:

检查您的适配器列表是否为空,您可以执行以下操作

if (list != null) {
 listview.setAdapter(new AdapterClass(list, context));
}else{listview.setAdapter(null)}

考虑到您将所有数据从适配器加载到列表中

【讨论】:

  • 然后检查搜索结果
猜你喜欢
  • 1970-01-01
  • 2021-11-24
  • 1970-01-01
  • 2015-11-04
  • 2020-12-31
  • 2016-08-06
  • 2012-01-08
  • 2013-04-02
  • 2020-11-06
相关资源
最近更新 更多