如果结果为空,则在过滤时隐藏ListView listView

时间:2015-03-26 06:30:34

标签: android android-listview filtering android-arrayadapter

我遇到了一个问题。

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

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);

            } });

任何人都可以帮助我! ? 非常感谢提前......

2 个答案:

答案 0 :(得分: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);
     }

答案 1 :(得分:0)

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

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

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