My searchview/filter does nothing when typing inside












1














I have written code for an app that is designed to read XML and populate a listview with the data read. The data is stored in a containing class. I've created an adapter class as well as a custom filter. For some reason, when I type inside my searchView, nothing comes up! I've looked all over youtube/google for a few days and cannot find anything! please help!



private class ArticleAdapter extends ArrayAdapter<Article> implements Filterable {
private List<Article> completeList = new ArrayList<>(articles);

public ArticleAdapter(Context ctx) {

super(ctx, 0);

}

@NonNull
@Override
public Filter getFilter() {
return titleFilter;
}


public int getCount() {
return articles.size();
}

public Article getItem(int position) {
return articles.get(position);
}

public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.title_layout, parent, false);

}


TextView articleTitle = convertView.findViewById(R.id.titleId);

Article article = getItem(position);
if (article != null) {
articleTitle.setText(getItem(position).title);
}


return convertView;


}

Filter titleFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
List<Article> suggestions = new ArrayList<>();
if (constraint == null || constraint.length() == 0) {
suggestions.addAll(completeList);

} else {
String filterPattern = constraint.toString().toLowerCase().trim();
for (Article title : completeList) {
if (title.getTitle().toLowerCase().contains(filterPattern)) {
suggestions.add(title);
}
}
}
results.values = suggestions;
results.count = suggestions.size();

return results;
}

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
clear();
addAll((List) results.values);
notifyDataSetChanged();

}

@Override
public CharSequence convertResultToString(Object resultValue) {

return ((Article) resultValue).getTitle();
}
};

/* public long getItemId(int position) {
return getItem(position).id;
}
*/

}


}


And this is the onCreateOptionsMenu method from my ArticleList class (main class)



 @Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_search, menu);
item = menu.findItem(R.id.menuSearch);
searchView = (SearchView) item.getActionView();


searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
if (articles.isEmpty()) {

}
adapter.getFilter().filter(newText);

return false;
}
});
return super.onCreateOptionsMenu(menu);
}









share|improve this question





























    1














    I have written code for an app that is designed to read XML and populate a listview with the data read. The data is stored in a containing class. I've created an adapter class as well as a custom filter. For some reason, when I type inside my searchView, nothing comes up! I've looked all over youtube/google for a few days and cannot find anything! please help!



    private class ArticleAdapter extends ArrayAdapter<Article> implements Filterable {
    private List<Article> completeList = new ArrayList<>(articles);

    public ArticleAdapter(Context ctx) {

    super(ctx, 0);

    }

    @NonNull
    @Override
    public Filter getFilter() {
    return titleFilter;
    }


    public int getCount() {
    return articles.size();
    }

    public Article getItem(int position) {
    return articles.get(position);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
    convertView = LayoutInflater.from(getContext()).inflate(R.layout.title_layout, parent, false);

    }


    TextView articleTitle = convertView.findViewById(R.id.titleId);

    Article article = getItem(position);
    if (article != null) {
    articleTitle.setText(getItem(position).title);
    }


    return convertView;


    }

    Filter titleFilter = new Filter() {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
    FilterResults results = new FilterResults();
    List<Article> suggestions = new ArrayList<>();
    if (constraint == null || constraint.length() == 0) {
    suggestions.addAll(completeList);

    } else {
    String filterPattern = constraint.toString().toLowerCase().trim();
    for (Article title : completeList) {
    if (title.getTitle().toLowerCase().contains(filterPattern)) {
    suggestions.add(title);
    }
    }
    }
    results.values = suggestions;
    results.count = suggestions.size();

    return results;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
    clear();
    addAll((List) results.values);
    notifyDataSetChanged();

    }

    @Override
    public CharSequence convertResultToString(Object resultValue) {

    return ((Article) resultValue).getTitle();
    }
    };

    /* public long getItemId(int position) {
    return getItem(position).id;
    }
    */

    }


    }


    And this is the onCreateOptionsMenu method from my ArticleList class (main class)



     @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_search, menu);
    item = menu.findItem(R.id.menuSearch);
    searchView = (SearchView) item.getActionView();


    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String query) {
    return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
    if (articles.isEmpty()) {

    }
    adapter.getFilter().filter(newText);

    return false;
    }
    });
    return super.onCreateOptionsMenu(menu);
    }









    share|improve this question



























      1












      1








      1







      I have written code for an app that is designed to read XML and populate a listview with the data read. The data is stored in a containing class. I've created an adapter class as well as a custom filter. For some reason, when I type inside my searchView, nothing comes up! I've looked all over youtube/google for a few days and cannot find anything! please help!



      private class ArticleAdapter extends ArrayAdapter<Article> implements Filterable {
      private List<Article> completeList = new ArrayList<>(articles);

      public ArticleAdapter(Context ctx) {

      super(ctx, 0);

      }

      @NonNull
      @Override
      public Filter getFilter() {
      return titleFilter;
      }


      public int getCount() {
      return articles.size();
      }

      public Article getItem(int position) {
      return articles.get(position);
      }

      public View getView(int position, View convertView, ViewGroup parent) {
      if (convertView == null) {
      convertView = LayoutInflater.from(getContext()).inflate(R.layout.title_layout, parent, false);

      }


      TextView articleTitle = convertView.findViewById(R.id.titleId);

      Article article = getItem(position);
      if (article != null) {
      articleTitle.setText(getItem(position).title);
      }


      return convertView;


      }

      Filter titleFilter = new Filter() {
      @Override
      protected FilterResults performFiltering(CharSequence constraint) {
      FilterResults results = new FilterResults();
      List<Article> suggestions = new ArrayList<>();
      if (constraint == null || constraint.length() == 0) {
      suggestions.addAll(completeList);

      } else {
      String filterPattern = constraint.toString().toLowerCase().trim();
      for (Article title : completeList) {
      if (title.getTitle().toLowerCase().contains(filterPattern)) {
      suggestions.add(title);
      }
      }
      }
      results.values = suggestions;
      results.count = suggestions.size();

      return results;
      }

      @Override
      protected void publishResults(CharSequence constraint, FilterResults results) {
      clear();
      addAll((List) results.values);
      notifyDataSetChanged();

      }

      @Override
      public CharSequence convertResultToString(Object resultValue) {

      return ((Article) resultValue).getTitle();
      }
      };

      /* public long getItemId(int position) {
      return getItem(position).id;
      }
      */

      }


      }


      And this is the onCreateOptionsMenu method from my ArticleList class (main class)



       @Override
      public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.menu_search, menu);
      item = menu.findItem(R.id.menuSearch);
      searchView = (SearchView) item.getActionView();


      searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
      @Override
      public boolean onQueryTextSubmit(String query) {
      return false;
      }

      @Override
      public boolean onQueryTextChange(String newText) {
      if (articles.isEmpty()) {

      }
      adapter.getFilter().filter(newText);

      return false;
      }
      });
      return super.onCreateOptionsMenu(menu);
      }









      share|improve this question















      I have written code for an app that is designed to read XML and populate a listview with the data read. The data is stored in a containing class. I've created an adapter class as well as a custom filter. For some reason, when I type inside my searchView, nothing comes up! I've looked all over youtube/google for a few days and cannot find anything! please help!



      private class ArticleAdapter extends ArrayAdapter<Article> implements Filterable {
      private List<Article> completeList = new ArrayList<>(articles);

      public ArticleAdapter(Context ctx) {

      super(ctx, 0);

      }

      @NonNull
      @Override
      public Filter getFilter() {
      return titleFilter;
      }


      public int getCount() {
      return articles.size();
      }

      public Article getItem(int position) {
      return articles.get(position);
      }

      public View getView(int position, View convertView, ViewGroup parent) {
      if (convertView == null) {
      convertView = LayoutInflater.from(getContext()).inflate(R.layout.title_layout, parent, false);

      }


      TextView articleTitle = convertView.findViewById(R.id.titleId);

      Article article = getItem(position);
      if (article != null) {
      articleTitle.setText(getItem(position).title);
      }


      return convertView;


      }

      Filter titleFilter = new Filter() {
      @Override
      protected FilterResults performFiltering(CharSequence constraint) {
      FilterResults results = new FilterResults();
      List<Article> suggestions = new ArrayList<>();
      if (constraint == null || constraint.length() == 0) {
      suggestions.addAll(completeList);

      } else {
      String filterPattern = constraint.toString().toLowerCase().trim();
      for (Article title : completeList) {
      if (title.getTitle().toLowerCase().contains(filterPattern)) {
      suggestions.add(title);
      }
      }
      }
      results.values = suggestions;
      results.count = suggestions.size();

      return results;
      }

      @Override
      protected void publishResults(CharSequence constraint, FilterResults results) {
      clear();
      addAll((List) results.values);
      notifyDataSetChanged();

      }

      @Override
      public CharSequence convertResultToString(Object resultValue) {

      return ((Article) resultValue).getTitle();
      }
      };

      /* public long getItemId(int position) {
      return getItem(position).id;
      }
      */

      }


      }


      And this is the onCreateOptionsMenu method from my ArticleList class (main class)



       @Override
      public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.menu_search, menu);
      item = menu.findItem(R.id.menuSearch);
      searchView = (SearchView) item.getActionView();


      searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
      @Override
      public boolean onQueryTextSubmit(String query) {
      return false;
      }

      @Override
      public boolean onQueryTextChange(String newText) {
      if (articles.isEmpty()) {

      }
      adapter.getFilter().filter(newText);

      return false;
      }
      });
      return super.onCreateOptionsMenu(menu);
      }






      java android listview android-arrayadapter searchview






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 1:16









      Andrew Thompson

      153k27163338




      153k27163338










      asked Nov 21 '18 at 0:17









      Chris Cherryholme

      61




      61
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I had a list of customers where I had to search a customer by their name.So here is the code.I hope it helps. :)



          //Defined this two list global



          private List customerList = new ArrayList<>();



          private List searchCustomerList = new ArrayList<>();



          In onCreate() I call this method : addTextListener();



          private void addTextListener()
          {



              try {

          searchBar.addTextChangedListener(new TextWatcher() {

          public void afterTextChanged(Editable s) {
          }

          public void beforeTextChanged(CharSequence s, int start, int count, int after) {
          }

          public void onTextChanged(CharSequence query, int start, int before, int count) {

          query = query.toString().toLowerCase();
          searchCustomerList.clear();
          for (int i = 0; i < customerList.size(); i++) {
          if (customerList.get(i) != null) {
          String text = customerList.get(i).getCustomerName().toLowerCase();
          if (text.toString().contains(query)) {
          searchCustomerList.add(customerList.get(i));
          }
          }
          }
          Log.d("filterflower", "size: " + searchCustomerList.size());
          customerListAdapter.updateList(searchCustomerList); // data set changed
          }
          });

          }catch (Exception e){
          e.printStackTrace();
          }
          }


          //Here is the updateList Method in my recycler adapter



           public void updateList(List<CustomerListDataModel> searchCustomerList) {
          customerList = searchCustomerList;
          notifyDataSetChanged();
          }





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403548%2fmy-searchview-filter-does-nothing-when-typing-inside%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I had a list of customers where I had to search a customer by their name.So here is the code.I hope it helps. :)



            //Defined this two list global



            private List customerList = new ArrayList<>();



            private List searchCustomerList = new ArrayList<>();



            In onCreate() I call this method : addTextListener();



            private void addTextListener()
            {



                try {

            searchBar.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            public void onTextChanged(CharSequence query, int start, int before, int count) {

            query = query.toString().toLowerCase();
            searchCustomerList.clear();
            for (int i = 0; i < customerList.size(); i++) {
            if (customerList.get(i) != null) {
            String text = customerList.get(i).getCustomerName().toLowerCase();
            if (text.toString().contains(query)) {
            searchCustomerList.add(customerList.get(i));
            }
            }
            }
            Log.d("filterflower", "size: " + searchCustomerList.size());
            customerListAdapter.updateList(searchCustomerList); // data set changed
            }
            });

            }catch (Exception e){
            e.printStackTrace();
            }
            }


            //Here is the updateList Method in my recycler adapter



             public void updateList(List<CustomerListDataModel> searchCustomerList) {
            customerList = searchCustomerList;
            notifyDataSetChanged();
            }





            share|improve this answer


























              0














              I had a list of customers where I had to search a customer by their name.So here is the code.I hope it helps. :)



              //Defined this two list global



              private List customerList = new ArrayList<>();



              private List searchCustomerList = new ArrayList<>();



              In onCreate() I call this method : addTextListener();



              private void addTextListener()
              {



                  try {

              searchBar.addTextChangedListener(new TextWatcher() {

              public void afterTextChanged(Editable s) {
              }

              public void beforeTextChanged(CharSequence s, int start, int count, int after) {
              }

              public void onTextChanged(CharSequence query, int start, int before, int count) {

              query = query.toString().toLowerCase();
              searchCustomerList.clear();
              for (int i = 0; i < customerList.size(); i++) {
              if (customerList.get(i) != null) {
              String text = customerList.get(i).getCustomerName().toLowerCase();
              if (text.toString().contains(query)) {
              searchCustomerList.add(customerList.get(i));
              }
              }
              }
              Log.d("filterflower", "size: " + searchCustomerList.size());
              customerListAdapter.updateList(searchCustomerList); // data set changed
              }
              });

              }catch (Exception e){
              e.printStackTrace();
              }
              }


              //Here is the updateList Method in my recycler adapter



               public void updateList(List<CustomerListDataModel> searchCustomerList) {
              customerList = searchCustomerList;
              notifyDataSetChanged();
              }





              share|improve this answer
























                0












                0








                0






                I had a list of customers where I had to search a customer by their name.So here is the code.I hope it helps. :)



                //Defined this two list global



                private List customerList = new ArrayList<>();



                private List searchCustomerList = new ArrayList<>();



                In onCreate() I call this method : addTextListener();



                private void addTextListener()
                {



                    try {

                searchBar.addTextChangedListener(new TextWatcher() {

                public void afterTextChanged(Editable s) {
                }

                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                public void onTextChanged(CharSequence query, int start, int before, int count) {

                query = query.toString().toLowerCase();
                searchCustomerList.clear();
                for (int i = 0; i < customerList.size(); i++) {
                if (customerList.get(i) != null) {
                String text = customerList.get(i).getCustomerName().toLowerCase();
                if (text.toString().contains(query)) {
                searchCustomerList.add(customerList.get(i));
                }
                }
                }
                Log.d("filterflower", "size: " + searchCustomerList.size());
                customerListAdapter.updateList(searchCustomerList); // data set changed
                }
                });

                }catch (Exception e){
                e.printStackTrace();
                }
                }


                //Here is the updateList Method in my recycler adapter



                 public void updateList(List<CustomerListDataModel> searchCustomerList) {
                customerList = searchCustomerList;
                notifyDataSetChanged();
                }





                share|improve this answer












                I had a list of customers where I had to search a customer by their name.So here is the code.I hope it helps. :)



                //Defined this two list global



                private List customerList = new ArrayList<>();



                private List searchCustomerList = new ArrayList<>();



                In onCreate() I call this method : addTextListener();



                private void addTextListener()
                {



                    try {

                searchBar.addTextChangedListener(new TextWatcher() {

                public void afterTextChanged(Editable s) {
                }

                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                public void onTextChanged(CharSequence query, int start, int before, int count) {

                query = query.toString().toLowerCase();
                searchCustomerList.clear();
                for (int i = 0; i < customerList.size(); i++) {
                if (customerList.get(i) != null) {
                String text = customerList.get(i).getCustomerName().toLowerCase();
                if (text.toString().contains(query)) {
                searchCustomerList.add(customerList.get(i));
                }
                }
                }
                Log.d("filterflower", "size: " + searchCustomerList.size());
                customerListAdapter.updateList(searchCustomerList); // data set changed
                }
                });

                }catch (Exception e){
                e.printStackTrace();
                }
                }


                //Here is the updateList Method in my recycler adapter



                 public void updateList(List<CustomerListDataModel> searchCustomerList) {
                customerList = searchCustomerList;
                notifyDataSetChanged();
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 '18 at 6:35









                Indranil Chatterjee

                213




                213






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403548%2fmy-searchview-filter-does-nothing-when-typing-inside%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Costa Masnaga

                    Fotorealismo

                    Sidney Franklin