Search code examples
javaandroidfirebasesearchbar

I want to implement a search bar which will accept a string then either it will filter the populated listview or search that string on server


In my android app Currently, am using a search bar which accepts a string and filters the populated list by using TextWatcher and stuff. But now if I got the results blank from the list then I need to search same string on the server side. Backend Firebase. How to do this any idea? Let me know if you need any further details.

 searchview.setOnQueryTextListener(new 
    android.widget.SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            Log.d("new text 2",query);
            adapter.getFilter().filter(query);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            Log.d("new text 1",newText);
            if (TextUtils.isEmpty(newText)) {
                listView.clearTextFilter();
            }
            adapter.getFilter().filter(newText);
            listView.setFilterText(newText);
            return false;
        }
    });



    @NonNull
       public Filter getFilter() {
          if (customFilter == null) {
              customFilter = new CustomFilter();
          }
          return customFilter;
      }


    //the class that creates the filter
    private class CustomFilter extends Filter {


        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            Log.d("inside perform filter",constraint.toString());
            FilterResults results = new FilterResults();
            // We implement here the filter logic
            if (constraint == null || constraint.length() == 0) {
                // No filter implemented we return all the list

                results.values = conn_List;
                results.count = conn_List.size();
            } else {
                // We perform filtering operation
                List<Connection_Class> newList = new ArrayList<>();

                for (Connection_Class p : conn_List) {
                    if (p.getName().toUpperCase().startsWith(constraint.toString().toUpperCase()))
                        newList.add(p);

                }

                results.values = newList;
                results.count = newList.size();

            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            Log.d("inside publishing",constraint.toString()+" "+ results.count);
            // Now we have to inform the adapter about the new list filtered
            if (results.count == 0) {

                notifyDataSetChanged();
                //notifyDataSetInvalidated();
                //Context context = getActivity();
                //CharSequence text = "Please Search Something Else...!";
                //int duration = Toast.LENGTH_SHORT;

                //Toast toast = Toast.makeText(context, text, duration);
            } else {
                connection_Array = (ArrayList<Connection_Class>) 
      results.values;
                Log.d("inside results",constraint.toString()+" "+ 
            connection_Array.size());
                  notifyDataSetInvalidated();
 }

        }


    }

The Question is edited. Take a look for now what I have done. This code filters the string within the populated result. Now If the string is not in the list it should start searching for it on sever. Like an ordinary search. Thanks for your help guys. Much appreciated.


Solution

  • As mentioned in comment i tried firing the query for searching results on server when i get filtered result list size 0. And then updating the adapter on that result.