Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display "No Result" in filterable ListView?

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?
like image 681
stuckedoverflow Avatar asked May 27 '11 02:05

stuckedoverflow


3 Answers

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.

like image 171
User Avatar answered Nov 16 '22 06:11

User


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);
    }
like image 2
Askarc Ali Avatar answered Nov 16 '22 06:11

Askarc Ali


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.

like image 1
DynamicMind Avatar answered Nov 16 '22 06:11

DynamicMind