How to display "No Result" in a filtered ListView?

I have ListViewand EditText. I implement addTextChangedListeneron EditTextup content filterListView .

leftList.setTextFilterEnabled(true);
et_search.addTextChangedListener(filterTextWatcher);

and then 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());    

    }
};

Condition:

  • I have 10 items in ListView.

Question:

  • When I first type the first character, why does it watcherAdapter.getCount()return 10(as the initial one) to ListView instead of the return result of the filter result? watcherAdapter.getCount() it seems that a click for the displayed result in ListViewthe message "late" appears.
  • How can I show "No Result"in ListViewwhen there are no matching results when I type EditText?
+5
5

, , - ... , , , . , .

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

. , , , , , . , . , .

+1

, TextView , , :

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

, GONE. , VISIBLE.

, , ,

publishResults(CharSequence constraint, FilterResults results)

, , FilterResults.

publishResults noResultsFoundView. . , . , , intance.

+1

, . ,

 @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);
    }
+1

All Articles