How to display "No Result" in filterable ListView?
Asked Answered
C

5

6

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?
Convery answered 27/5, 2011 at 2:47 Comment(1)
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.Convery
C
0

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

Convery answered 7/12, 2011 at 3:7 Comment(0)
O
3

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.

Organizer answered 4/5, 2012 at 10:59 Comment(0)
H
2

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);
    }
Hindemith answered 21/12, 2016 at 7:21 Comment(0)
D
1
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.

Dendritic answered 27/5, 2011 at 5:51 Comment(1)
Dont really get it. You put this onTextChanged? I suppose fillMaps in your case is also a click late right?Convery
C
0

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

Convery answered 7/12, 2011 at 3:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.