Add Filter in SearchView with Room and RecyleView











up vote
0
down vote

favorite












(google translate)



Intro:
I have searched for this solution, but did not find it. If there is a duplicate of this problem, please let me know. thanks.



Problem:
I created a storage application with the Room database and RecycleView. Then I looked for ways to add Search to my data. Everything went smoothly, but there was one problem.
If the search word is reduced, then the search is limited to visible data. (Look at the screenshot)




  1. Normal

  2. Search is done (Normal)

  3. Empty word

  4. Search again and The result

  5. GIF Image


I hope you understand.
Please help me. I have seen a lot of tutorials but nothing works.



thank you



My Code:
Filter in my Adapter



public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
List<Barang> filteredList = new ArrayList<>();
if (charString.isEmpty()) {
filteredList.addAll(namaBarang);
} else {
for (Barang row : namaBarang) {

// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
if (row.getNamaBarang().toLowerCase().contains(charString.toLowerCase())) {
filteredList.add(row);
}
}

}

FilterResults filterResults = new FilterResults();
filterResults.values = filteredList;
return filterResults;
}

@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
namaBarang = (ArrayList<Barang>) filterResults.values;
notifyDataSetChanged();
}
};
}


MainActivity



   @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
final MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(this);
return true;
}

@Override
public boolean onQueryTextSubmit(String query) {
adapter.getFilter().filter(query);
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
// t = newText.;

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


And I have this too in my Adapter:



void setBarangs(List<Barang> barangs){
namaBarang = barangs;
notifyDataSetChanged();
}









share|improve this question




























    up vote
    0
    down vote

    favorite












    (google translate)



    Intro:
    I have searched for this solution, but did not find it. If there is a duplicate of this problem, please let me know. thanks.



    Problem:
    I created a storage application with the Room database and RecycleView. Then I looked for ways to add Search to my data. Everything went smoothly, but there was one problem.
    If the search word is reduced, then the search is limited to visible data. (Look at the screenshot)




    1. Normal

    2. Search is done (Normal)

    3. Empty word

    4. Search again and The result

    5. GIF Image


    I hope you understand.
    Please help me. I have seen a lot of tutorials but nothing works.



    thank you



    My Code:
    Filter in my Adapter



    public Filter getFilter() {
    return new Filter() {
    @Override
    protected FilterResults performFiltering(CharSequence charSequence) {
    String charString = charSequence.toString();
    List<Barang> filteredList = new ArrayList<>();
    if (charString.isEmpty()) {
    filteredList.addAll(namaBarang);
    } else {
    for (Barang row : namaBarang) {

    // name match condition. this might differ depending on your requirement
    // here we are looking for name or phone number match
    if (row.getNamaBarang().toLowerCase().contains(charString.toLowerCase())) {
    filteredList.add(row);
    }
    }

    }

    FilterResults filterResults = new FilterResults();
    filterResults.values = filteredList;
    return filterResults;
    }

    @Override
    protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
    namaBarang = (ArrayList<Barang>) filterResults.values;
    notifyDataSetChanged();
    }
    };
    }


    MainActivity



       @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    final MenuItem searchItem = menu.findItem(R.id.action_search);
    final SearchView searchView = (SearchView) searchItem.getActionView();
    searchView.setOnQueryTextListener(this);
    return true;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
    adapter.getFilter().filter(query);
    return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
    // t = newText.;

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


    And I have this too in my Adapter:



    void setBarangs(List<Barang> barangs){
    namaBarang = barangs;
    notifyDataSetChanged();
    }









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      (google translate)



      Intro:
      I have searched for this solution, but did not find it. If there is a duplicate of this problem, please let me know. thanks.



      Problem:
      I created a storage application with the Room database and RecycleView. Then I looked for ways to add Search to my data. Everything went smoothly, but there was one problem.
      If the search word is reduced, then the search is limited to visible data. (Look at the screenshot)




      1. Normal

      2. Search is done (Normal)

      3. Empty word

      4. Search again and The result

      5. GIF Image


      I hope you understand.
      Please help me. I have seen a lot of tutorials but nothing works.



      thank you



      My Code:
      Filter in my Adapter



      public Filter getFilter() {
      return new Filter() {
      @Override
      protected FilterResults performFiltering(CharSequence charSequence) {
      String charString = charSequence.toString();
      List<Barang> filteredList = new ArrayList<>();
      if (charString.isEmpty()) {
      filteredList.addAll(namaBarang);
      } else {
      for (Barang row : namaBarang) {

      // name match condition. this might differ depending on your requirement
      // here we are looking for name or phone number match
      if (row.getNamaBarang().toLowerCase().contains(charString.toLowerCase())) {
      filteredList.add(row);
      }
      }

      }

      FilterResults filterResults = new FilterResults();
      filterResults.values = filteredList;
      return filterResults;
      }

      @Override
      protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
      namaBarang = (ArrayList<Barang>) filterResults.values;
      notifyDataSetChanged();
      }
      };
      }


      MainActivity



         @Override
      public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_main, menu);
      final MenuItem searchItem = menu.findItem(R.id.action_search);
      final SearchView searchView = (SearchView) searchItem.getActionView();
      searchView.setOnQueryTextListener(this);
      return true;
      }

      @Override
      public boolean onQueryTextSubmit(String query) {
      adapter.getFilter().filter(query);
      return false;
      }

      @Override
      public boolean onQueryTextChange(String newText) {
      // t = newText.;

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


      And I have this too in my Adapter:



      void setBarangs(List<Barang> barangs){
      namaBarang = barangs;
      notifyDataSetChanged();
      }









      share|improve this question















      (google translate)



      Intro:
      I have searched for this solution, but did not find it. If there is a duplicate of this problem, please let me know. thanks.



      Problem:
      I created a storage application with the Room database and RecycleView. Then I looked for ways to add Search to my data. Everything went smoothly, but there was one problem.
      If the search word is reduced, then the search is limited to visible data. (Look at the screenshot)




      1. Normal

      2. Search is done (Normal)

      3. Empty word

      4. Search again and The result

      5. GIF Image


      I hope you understand.
      Please help me. I have seen a lot of tutorials but nothing works.



      thank you



      My Code:
      Filter in my Adapter



      public Filter getFilter() {
      return new Filter() {
      @Override
      protected FilterResults performFiltering(CharSequence charSequence) {
      String charString = charSequence.toString();
      List<Barang> filteredList = new ArrayList<>();
      if (charString.isEmpty()) {
      filteredList.addAll(namaBarang);
      } else {
      for (Barang row : namaBarang) {

      // name match condition. this might differ depending on your requirement
      // here we are looking for name or phone number match
      if (row.getNamaBarang().toLowerCase().contains(charString.toLowerCase())) {
      filteredList.add(row);
      }
      }

      }

      FilterResults filterResults = new FilterResults();
      filterResults.values = filteredList;
      return filterResults;
      }

      @Override
      protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
      namaBarang = (ArrayList<Barang>) filterResults.values;
      notifyDataSetChanged();
      }
      };
      }


      MainActivity



         @Override
      public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_main, menu);
      final MenuItem searchItem = menu.findItem(R.id.action_search);
      final SearchView searchView = (SearchView) searchItem.getActionView();
      searchView.setOnQueryTextListener(this);
      return true;
      }

      @Override
      public boolean onQueryTextSubmit(String query) {
      adapter.getFilter().filter(query);
      return false;
      }

      @Override
      public boolean onQueryTextChange(String newText) {
      // t = newText.;

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


      And I have this too in my Adapter:



      void setBarangs(List<Barang> barangs){
      namaBarang = barangs;
      notifyDataSetChanged();
      }






      android android-recyclerview searchview android-room android-filterable






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 7 at 23:26

























      asked Nov 7 at 14:36









      Jufry Ananta

      11




      11
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          My problem has been solved with this:



          @Override
          public Filter getFilter() {
          return new Filter() {
          @Override
          protected FilterResults performFiltering(CharSequence charSequence) {
          String charString = charSequence.toString();
          List<Barang> listHasilFilter = new ArrayList<>();
          List<Barang> listYgAkandiFilter = new ArrayList<>();
          listYgAkandiFilter.addAll(namaBarangC);
          if (charString.isEmpty()) {
          listHasilFilter = listYgAkandiFilter;

          // namaBarang = listYgAkandiFilter;
          Log.d(TAG, "" + namaBarang.size());
          } else {
          for (Barang row : namaBarangC) {

          // name match condition. this might differ depending on your requirement
          // here we are looking for name or phone number match
          if (row.getNamaBarang().toLowerCase().contains(charString.toLowerCase())) {
          listHasilFilter.add(row);
          }
          }

          }

          FilterResults hasilFilter = new FilterResults();
          hasilFilter.values = listHasilFilter;
          return hasilFilter;
          }

          @Override
          protected void publishResults(CharSequence charSequence, FilterResults hasilFilter) {
          namaBarang = (ArrayList<Barang>) hasilFilter.values;
          notifyDataSetChanged();
          }
          };
          }


          Thanks






          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',
            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%2f53191600%2fadd-filter-in-searchview-with-room-and-recyleview%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








            up vote
            0
            down vote













            My problem has been solved with this:



            @Override
            public Filter getFilter() {
            return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {
            String charString = charSequence.toString();
            List<Barang> listHasilFilter = new ArrayList<>();
            List<Barang> listYgAkandiFilter = new ArrayList<>();
            listYgAkandiFilter.addAll(namaBarangC);
            if (charString.isEmpty()) {
            listHasilFilter = listYgAkandiFilter;

            // namaBarang = listYgAkandiFilter;
            Log.d(TAG, "" + namaBarang.size());
            } else {
            for (Barang row : namaBarangC) {

            // name match condition. this might differ depending on your requirement
            // here we are looking for name or phone number match
            if (row.getNamaBarang().toLowerCase().contains(charString.toLowerCase())) {
            listHasilFilter.add(row);
            }
            }

            }

            FilterResults hasilFilter = new FilterResults();
            hasilFilter.values = listHasilFilter;
            return hasilFilter;
            }

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults hasilFilter) {
            namaBarang = (ArrayList<Barang>) hasilFilter.values;
            notifyDataSetChanged();
            }
            };
            }


            Thanks






            share|improve this answer

























              up vote
              0
              down vote













              My problem has been solved with this:



              @Override
              public Filter getFilter() {
              return new Filter() {
              @Override
              protected FilterResults performFiltering(CharSequence charSequence) {
              String charString = charSequence.toString();
              List<Barang> listHasilFilter = new ArrayList<>();
              List<Barang> listYgAkandiFilter = new ArrayList<>();
              listYgAkandiFilter.addAll(namaBarangC);
              if (charString.isEmpty()) {
              listHasilFilter = listYgAkandiFilter;

              // namaBarang = listYgAkandiFilter;
              Log.d(TAG, "" + namaBarang.size());
              } else {
              for (Barang row : namaBarangC) {

              // name match condition. this might differ depending on your requirement
              // here we are looking for name or phone number match
              if (row.getNamaBarang().toLowerCase().contains(charString.toLowerCase())) {
              listHasilFilter.add(row);
              }
              }

              }

              FilterResults hasilFilter = new FilterResults();
              hasilFilter.values = listHasilFilter;
              return hasilFilter;
              }

              @Override
              protected void publishResults(CharSequence charSequence, FilterResults hasilFilter) {
              namaBarang = (ArrayList<Barang>) hasilFilter.values;
              notifyDataSetChanged();
              }
              };
              }


              Thanks






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                My problem has been solved with this:



                @Override
                public Filter getFilter() {
                return new Filter() {
                @Override
                protected FilterResults performFiltering(CharSequence charSequence) {
                String charString = charSequence.toString();
                List<Barang> listHasilFilter = new ArrayList<>();
                List<Barang> listYgAkandiFilter = new ArrayList<>();
                listYgAkandiFilter.addAll(namaBarangC);
                if (charString.isEmpty()) {
                listHasilFilter = listYgAkandiFilter;

                // namaBarang = listYgAkandiFilter;
                Log.d(TAG, "" + namaBarang.size());
                } else {
                for (Barang row : namaBarangC) {

                // name match condition. this might differ depending on your requirement
                // here we are looking for name or phone number match
                if (row.getNamaBarang().toLowerCase().contains(charString.toLowerCase())) {
                listHasilFilter.add(row);
                }
                }

                }

                FilterResults hasilFilter = new FilterResults();
                hasilFilter.values = listHasilFilter;
                return hasilFilter;
                }

                @Override
                protected void publishResults(CharSequence charSequence, FilterResults hasilFilter) {
                namaBarang = (ArrayList<Barang>) hasilFilter.values;
                notifyDataSetChanged();
                }
                };
                }


                Thanks






                share|improve this answer












                My problem has been solved with this:



                @Override
                public Filter getFilter() {
                return new Filter() {
                @Override
                protected FilterResults performFiltering(CharSequence charSequence) {
                String charString = charSequence.toString();
                List<Barang> listHasilFilter = new ArrayList<>();
                List<Barang> listYgAkandiFilter = new ArrayList<>();
                listYgAkandiFilter.addAll(namaBarangC);
                if (charString.isEmpty()) {
                listHasilFilter = listYgAkandiFilter;

                // namaBarang = listYgAkandiFilter;
                Log.d(TAG, "" + namaBarang.size());
                } else {
                for (Barang row : namaBarangC) {

                // name match condition. this might differ depending on your requirement
                // here we are looking for name or phone number match
                if (row.getNamaBarang().toLowerCase().contains(charString.toLowerCase())) {
                listHasilFilter.add(row);
                }
                }

                }

                FilterResults hasilFilter = new FilterResults();
                hasilFilter.values = listHasilFilter;
                return hasilFilter;
                }

                @Override
                protected void publishResults(CharSequence charSequence, FilterResults hasilFilter) {
                namaBarang = (ArrayList<Barang>) hasilFilter.values;
                notifyDataSetChanged();
                }
                };
                }


                Thanks







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 8 at 8:21









                Jufry Ananta

                11




                11






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53191600%2fadd-filter-in-searchview-with-room-and-recyleview%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







                    這個網誌中的熱門文章

                    Xamarin.form Move up view when keyboard appear

                    Post-Redirect-Get with Spring WebFlux and Thymeleaf

                    Anylogic : not able to use stopDelay()