How to search list of data from firebase firestore using android searchView












0















I'm using the Cloud Firestore of Firebase to store users and the android Searchview to provide search fonctionnality. When a user search for "jonathan" for example, if he begin typing "j" i want to bring in all users with the name starting par "j". How can i achieve this ?



Here is what i have tried:



@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);

MenuItem search = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) search.getActionView();

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

@Override
public boolean onQueryTextSubmit(String query) {
//Toast.makeText(MainActivity.this, "SEARCH " + query, Toast.LENGTH_LONG).show();
searchUsers(query);
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
//Toast.makeText(MainActivity.this, "SEARCH " + newText, Toast.LENGTH_LONG).show();
searchUsers(newText);
return false;
}
});

return true;
}


Getting the user from Firebase:



private void searchUsers(String recherche) {
if(recherche.length() > 0)
recherche = recherche.substring(0,1).toUpperCase() + recherche.substring(1).toLowerCase();
listUsers = new ArrayList<>();

db.collection("users").whereGreaterThanOrEqualTo("name", recherche)
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot snapshots,
@Nullable FirebaseFirestoreException e) {
if (e != null) {
System.err.println("Listen failed:" + e);
return;
}
listUsers = new ArrayList<User>();

for (DocumentSnapshot doc : snapshots) {
User user = doc.toObject(User.class);
listUsers.add(user);
}
updateListUsers(listUsers);
}
});
}


This works only for the first letter "j" as soon as i add "ja" for example i still have all the "jonathan" displayed










share|improve this question























  • You can also check this out.

    – Alex Mamo
    Jan 3 at 18:18
















0















I'm using the Cloud Firestore of Firebase to store users and the android Searchview to provide search fonctionnality. When a user search for "jonathan" for example, if he begin typing "j" i want to bring in all users with the name starting par "j". How can i achieve this ?



Here is what i have tried:



@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);

MenuItem search = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) search.getActionView();

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

@Override
public boolean onQueryTextSubmit(String query) {
//Toast.makeText(MainActivity.this, "SEARCH " + query, Toast.LENGTH_LONG).show();
searchUsers(query);
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
//Toast.makeText(MainActivity.this, "SEARCH " + newText, Toast.LENGTH_LONG).show();
searchUsers(newText);
return false;
}
});

return true;
}


Getting the user from Firebase:



private void searchUsers(String recherche) {
if(recherche.length() > 0)
recherche = recherche.substring(0,1).toUpperCase() + recherche.substring(1).toLowerCase();
listUsers = new ArrayList<>();

db.collection("users").whereGreaterThanOrEqualTo("name", recherche)
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot snapshots,
@Nullable FirebaseFirestoreException e) {
if (e != null) {
System.err.println("Listen failed:" + e);
return;
}
listUsers = new ArrayList<User>();

for (DocumentSnapshot doc : snapshots) {
User user = doc.toObject(User.class);
listUsers.add(user);
}
updateListUsers(listUsers);
}
});
}


This works only for the first letter "j" as soon as i add "ja" for example i still have all the "jonathan" displayed










share|improve this question























  • You can also check this out.

    – Alex Mamo
    Jan 3 at 18:18














0












0








0








I'm using the Cloud Firestore of Firebase to store users and the android Searchview to provide search fonctionnality. When a user search for "jonathan" for example, if he begin typing "j" i want to bring in all users with the name starting par "j". How can i achieve this ?



Here is what i have tried:



@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);

MenuItem search = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) search.getActionView();

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

@Override
public boolean onQueryTextSubmit(String query) {
//Toast.makeText(MainActivity.this, "SEARCH " + query, Toast.LENGTH_LONG).show();
searchUsers(query);
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
//Toast.makeText(MainActivity.this, "SEARCH " + newText, Toast.LENGTH_LONG).show();
searchUsers(newText);
return false;
}
});

return true;
}


Getting the user from Firebase:



private void searchUsers(String recherche) {
if(recherche.length() > 0)
recherche = recherche.substring(0,1).toUpperCase() + recherche.substring(1).toLowerCase();
listUsers = new ArrayList<>();

db.collection("users").whereGreaterThanOrEqualTo("name", recherche)
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot snapshots,
@Nullable FirebaseFirestoreException e) {
if (e != null) {
System.err.println("Listen failed:" + e);
return;
}
listUsers = new ArrayList<User>();

for (DocumentSnapshot doc : snapshots) {
User user = doc.toObject(User.class);
listUsers.add(user);
}
updateListUsers(listUsers);
}
});
}


This works only for the first letter "j" as soon as i add "ja" for example i still have all the "jonathan" displayed










share|improve this question














I'm using the Cloud Firestore of Firebase to store users and the android Searchview to provide search fonctionnality. When a user search for "jonathan" for example, if he begin typing "j" i want to bring in all users with the name starting par "j". How can i achieve this ?



Here is what i have tried:



@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);

MenuItem search = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) search.getActionView();

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

@Override
public boolean onQueryTextSubmit(String query) {
//Toast.makeText(MainActivity.this, "SEARCH " + query, Toast.LENGTH_LONG).show();
searchUsers(query);
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
//Toast.makeText(MainActivity.this, "SEARCH " + newText, Toast.LENGTH_LONG).show();
searchUsers(newText);
return false;
}
});

return true;
}


Getting the user from Firebase:



private void searchUsers(String recherche) {
if(recherche.length() > 0)
recherche = recherche.substring(0,1).toUpperCase() + recherche.substring(1).toLowerCase();
listUsers = new ArrayList<>();

db.collection("users").whereGreaterThanOrEqualTo("name", recherche)
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot snapshots,
@Nullable FirebaseFirestoreException e) {
if (e != null) {
System.err.println("Listen failed:" + e);
return;
}
listUsers = new ArrayList<User>();

for (DocumentSnapshot doc : snapshots) {
User user = doc.toObject(User.class);
listUsers.add(user);
}
updateListUsers(listUsers);
}
});
}


This works only for the first letter "j" as soon as i add "ja" for example i still have all the "jonathan" displayed







android firebase google-cloud-firestore






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 31 '18 at 21:11









Moussa DialloMoussa Diallo

12




12













  • You can also check this out.

    – Alex Mamo
    Jan 3 at 18:18



















  • You can also check this out.

    – Alex Mamo
    Jan 3 at 18:18

















You can also check this out.

– Alex Mamo
Jan 3 at 18:18





You can also check this out.

– Alex Mamo
Jan 3 at 18:18












2 Answers
2






active

oldest

votes


















0














My way of searching is rather inefficient as am using two recyclerviews, one is hidden and the other with the data visible. All data in the document is fetched to an array and i query the array in real time as you want. The data remains synced always so it seems real time.Not a good practice but it gets the job done for me.If you need further help I can create a gist for you in that.
The proper way would be to use this official link on search on exactly what you need
[https://firebase.google.com/docs/firestore/solutions/search][1]






share|improve this answer































    0














    Thanks to the suggestion of @Oby if found the solution. In fact i dont really need to query the database every time the search is triggered since i have the list of users. I just have to make the search on the list like this:
    We get the list first:



    private void getUsers() {
    db.collection("users").whereEqualTo("etat", 1)
    .addSnapshotListener(new EventListener<QuerySnapshot>() {
    @Override
    public void onEvent(@Nullable QuerySnapshot snapshots,
    @Nullable FirebaseFirestoreException e) {
    if (e != null) {
    System.err.println("Listen failed:" + e);
    return;
    }
    listUsers = new ArrayList<User>();

    for (DocumentSnapshot doc : snapshots) {
    User user = doc.toObject(User.class);
    listUsers.add(user);
    }
    updateListUsers(listUsers);
    }
    });
    }


    Here is the search function:



    private void searchUsers(String recherche) {
    if (recherche.length() > 0)
    recherche = recherche.substring(0, 1).toUpperCase() + recherche.substring(1).toLowerCase();

    ArrayList<User> results = new ArrayList<>();
    for(User user : listUsers){
    if(user.getName() != null && user.getName().contains(recherche)){
    results.add(user);
    }
    }
    updateListUsers(results);
    }


    Here i notify the the Adapter of the RecyclerView that the data changed:



    private void updateListUsers(ArrayList<User> listUsers) {

    // Sort the list by date
    Collections.sort(listUsers, new Comparator<User>() {
    @Override
    public int compare(User o1, User o2) {
    int res = -1;
    if (o1.getDate() > (o2.getDate())) {
    res = 1;
    }
    return res;
    }
    });

    userRecyclerAdapter = new UserRecyclerAdapter(listUsers, InvitationActivity.this, this);
    rvUsers.setNestedScrollingEnabled(false);
    rvUsers.setAdapter(userRecyclerAdapter);
    layoutManagerUser = new LinearLayoutManager(getApplicationContext());
    rvUsers.setLayoutManager(layoutManagerUser);
    userRecyclerAdapter.notifyDataSetChanged();
    }


    And of course the SearchView:



     @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);

    MenuItem search = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) search.getActionView();

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

    @Override
    public boolean onQueryTextSubmit(String query) {
    //Toast.makeText(MainActivity.this, "SEARCH " + query, Toast.LENGTH_LONG).show();
    searchUsers(query);
    return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
    //Toast.makeText(MainActivity.this, "SEARCH " + newText, Toast.LENGTH_LONG).show();
    searchUsers(newText);
    return false;
    }
    });

    return true;
    }





    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%2f53991448%2fhow-to-search-list-of-data-from-firebase-firestore-using-android-searchview%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      My way of searching is rather inefficient as am using two recyclerviews, one is hidden and the other with the data visible. All data in the document is fetched to an array and i query the array in real time as you want. The data remains synced always so it seems real time.Not a good practice but it gets the job done for me.If you need further help I can create a gist for you in that.
      The proper way would be to use this official link on search on exactly what you need
      [https://firebase.google.com/docs/firestore/solutions/search][1]






      share|improve this answer




























        0














        My way of searching is rather inefficient as am using two recyclerviews, one is hidden and the other with the data visible. All data in the document is fetched to an array and i query the array in real time as you want. The data remains synced always so it seems real time.Not a good practice but it gets the job done for me.If you need further help I can create a gist for you in that.
        The proper way would be to use this official link on search on exactly what you need
        [https://firebase.google.com/docs/firestore/solutions/search][1]






        share|improve this answer


























          0












          0








          0







          My way of searching is rather inefficient as am using two recyclerviews, one is hidden and the other with the data visible. All data in the document is fetched to an array and i query the array in real time as you want. The data remains synced always so it seems real time.Not a good practice but it gets the job done for me.If you need further help I can create a gist for you in that.
          The proper way would be to use this official link on search on exactly what you need
          [https://firebase.google.com/docs/firestore/solutions/search][1]






          share|improve this answer













          My way of searching is rather inefficient as am using two recyclerviews, one is hidden and the other with the data visible. All data in the document is fetched to an array and i query the array in real time as you want. The data remains synced always so it seems real time.Not a good practice but it gets the job done for me.If you need further help I can create a gist for you in that.
          The proper way would be to use this official link on search on exactly what you need
          [https://firebase.google.com/docs/firestore/solutions/search][1]







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 31 '18 at 23:29









          Oby Oby

          11




          11

























              0














              Thanks to the suggestion of @Oby if found the solution. In fact i dont really need to query the database every time the search is triggered since i have the list of users. I just have to make the search on the list like this:
              We get the list first:



              private void getUsers() {
              db.collection("users").whereEqualTo("etat", 1)
              .addSnapshotListener(new EventListener<QuerySnapshot>() {
              @Override
              public void onEvent(@Nullable QuerySnapshot snapshots,
              @Nullable FirebaseFirestoreException e) {
              if (e != null) {
              System.err.println("Listen failed:" + e);
              return;
              }
              listUsers = new ArrayList<User>();

              for (DocumentSnapshot doc : snapshots) {
              User user = doc.toObject(User.class);
              listUsers.add(user);
              }
              updateListUsers(listUsers);
              }
              });
              }


              Here is the search function:



              private void searchUsers(String recherche) {
              if (recherche.length() > 0)
              recherche = recherche.substring(0, 1).toUpperCase() + recherche.substring(1).toLowerCase();

              ArrayList<User> results = new ArrayList<>();
              for(User user : listUsers){
              if(user.getName() != null && user.getName().contains(recherche)){
              results.add(user);
              }
              }
              updateListUsers(results);
              }


              Here i notify the the Adapter of the RecyclerView that the data changed:



              private void updateListUsers(ArrayList<User> listUsers) {

              // Sort the list by date
              Collections.sort(listUsers, new Comparator<User>() {
              @Override
              public int compare(User o1, User o2) {
              int res = -1;
              if (o1.getDate() > (o2.getDate())) {
              res = 1;
              }
              return res;
              }
              });

              userRecyclerAdapter = new UserRecyclerAdapter(listUsers, InvitationActivity.this, this);
              rvUsers.setNestedScrollingEnabled(false);
              rvUsers.setAdapter(userRecyclerAdapter);
              layoutManagerUser = new LinearLayoutManager(getApplicationContext());
              rvUsers.setLayoutManager(layoutManagerUser);
              userRecyclerAdapter.notifyDataSetChanged();
              }


              And of course the SearchView:



               @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);

              MenuItem search = menu.findItem(R.id.action_search);
              SearchView searchView = (SearchView) search.getActionView();

              searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

              @Override
              public boolean onQueryTextSubmit(String query) {
              //Toast.makeText(MainActivity.this, "SEARCH " + query, Toast.LENGTH_LONG).show();
              searchUsers(query);
              return false;
              }

              @Override
              public boolean onQueryTextChange(String newText) {
              //Toast.makeText(MainActivity.this, "SEARCH " + newText, Toast.LENGTH_LONG).show();
              searchUsers(newText);
              return false;
              }
              });

              return true;
              }





              share|improve this answer




























                0














                Thanks to the suggestion of @Oby if found the solution. In fact i dont really need to query the database every time the search is triggered since i have the list of users. I just have to make the search on the list like this:
                We get the list first:



                private void getUsers() {
                db.collection("users").whereEqualTo("etat", 1)
                .addSnapshotListener(new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(@Nullable QuerySnapshot snapshots,
                @Nullable FirebaseFirestoreException e) {
                if (e != null) {
                System.err.println("Listen failed:" + e);
                return;
                }
                listUsers = new ArrayList<User>();

                for (DocumentSnapshot doc : snapshots) {
                User user = doc.toObject(User.class);
                listUsers.add(user);
                }
                updateListUsers(listUsers);
                }
                });
                }


                Here is the search function:



                private void searchUsers(String recherche) {
                if (recherche.length() > 0)
                recherche = recherche.substring(0, 1).toUpperCase() + recherche.substring(1).toLowerCase();

                ArrayList<User> results = new ArrayList<>();
                for(User user : listUsers){
                if(user.getName() != null && user.getName().contains(recherche)){
                results.add(user);
                }
                }
                updateListUsers(results);
                }


                Here i notify the the Adapter of the RecyclerView that the data changed:



                private void updateListUsers(ArrayList<User> listUsers) {

                // Sort the list by date
                Collections.sort(listUsers, new Comparator<User>() {
                @Override
                public int compare(User o1, User o2) {
                int res = -1;
                if (o1.getDate() > (o2.getDate())) {
                res = 1;
                }
                return res;
                }
                });

                userRecyclerAdapter = new UserRecyclerAdapter(listUsers, InvitationActivity.this, this);
                rvUsers.setNestedScrollingEnabled(false);
                rvUsers.setAdapter(userRecyclerAdapter);
                layoutManagerUser = new LinearLayoutManager(getApplicationContext());
                rvUsers.setLayoutManager(layoutManagerUser);
                userRecyclerAdapter.notifyDataSetChanged();
                }


                And of course the SearchView:



                 @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);

                MenuItem search = menu.findItem(R.id.action_search);
                SearchView searchView = (SearchView) search.getActionView();

                searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

                @Override
                public boolean onQueryTextSubmit(String query) {
                //Toast.makeText(MainActivity.this, "SEARCH " + query, Toast.LENGTH_LONG).show();
                searchUsers(query);
                return false;
                }

                @Override
                public boolean onQueryTextChange(String newText) {
                //Toast.makeText(MainActivity.this, "SEARCH " + newText, Toast.LENGTH_LONG).show();
                searchUsers(newText);
                return false;
                }
                });

                return true;
                }





                share|improve this answer


























                  0












                  0








                  0







                  Thanks to the suggestion of @Oby if found the solution. In fact i dont really need to query the database every time the search is triggered since i have the list of users. I just have to make the search on the list like this:
                  We get the list first:



                  private void getUsers() {
                  db.collection("users").whereEqualTo("etat", 1)
                  .addSnapshotListener(new EventListener<QuerySnapshot>() {
                  @Override
                  public void onEvent(@Nullable QuerySnapshot snapshots,
                  @Nullable FirebaseFirestoreException e) {
                  if (e != null) {
                  System.err.println("Listen failed:" + e);
                  return;
                  }
                  listUsers = new ArrayList<User>();

                  for (DocumentSnapshot doc : snapshots) {
                  User user = doc.toObject(User.class);
                  listUsers.add(user);
                  }
                  updateListUsers(listUsers);
                  }
                  });
                  }


                  Here is the search function:



                  private void searchUsers(String recherche) {
                  if (recherche.length() > 0)
                  recherche = recherche.substring(0, 1).toUpperCase() + recherche.substring(1).toLowerCase();

                  ArrayList<User> results = new ArrayList<>();
                  for(User user : listUsers){
                  if(user.getName() != null && user.getName().contains(recherche)){
                  results.add(user);
                  }
                  }
                  updateListUsers(results);
                  }


                  Here i notify the the Adapter of the RecyclerView that the data changed:



                  private void updateListUsers(ArrayList<User> listUsers) {

                  // Sort the list by date
                  Collections.sort(listUsers, new Comparator<User>() {
                  @Override
                  public int compare(User o1, User o2) {
                  int res = -1;
                  if (o1.getDate() > (o2.getDate())) {
                  res = 1;
                  }
                  return res;
                  }
                  });

                  userRecyclerAdapter = new UserRecyclerAdapter(listUsers, InvitationActivity.this, this);
                  rvUsers.setNestedScrollingEnabled(false);
                  rvUsers.setAdapter(userRecyclerAdapter);
                  layoutManagerUser = new LinearLayoutManager(getApplicationContext());
                  rvUsers.setLayoutManager(layoutManagerUser);
                  userRecyclerAdapter.notifyDataSetChanged();
                  }


                  And of course the SearchView:



                   @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);

                  MenuItem search = menu.findItem(R.id.action_search);
                  SearchView searchView = (SearchView) search.getActionView();

                  searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

                  @Override
                  public boolean onQueryTextSubmit(String query) {
                  //Toast.makeText(MainActivity.this, "SEARCH " + query, Toast.LENGTH_LONG).show();
                  searchUsers(query);
                  return false;
                  }

                  @Override
                  public boolean onQueryTextChange(String newText) {
                  //Toast.makeText(MainActivity.this, "SEARCH " + newText, Toast.LENGTH_LONG).show();
                  searchUsers(newText);
                  return false;
                  }
                  });

                  return true;
                  }





                  share|improve this answer













                  Thanks to the suggestion of @Oby if found the solution. In fact i dont really need to query the database every time the search is triggered since i have the list of users. I just have to make the search on the list like this:
                  We get the list first:



                  private void getUsers() {
                  db.collection("users").whereEqualTo("etat", 1)
                  .addSnapshotListener(new EventListener<QuerySnapshot>() {
                  @Override
                  public void onEvent(@Nullable QuerySnapshot snapshots,
                  @Nullable FirebaseFirestoreException e) {
                  if (e != null) {
                  System.err.println("Listen failed:" + e);
                  return;
                  }
                  listUsers = new ArrayList<User>();

                  for (DocumentSnapshot doc : snapshots) {
                  User user = doc.toObject(User.class);
                  listUsers.add(user);
                  }
                  updateListUsers(listUsers);
                  }
                  });
                  }


                  Here is the search function:



                  private void searchUsers(String recherche) {
                  if (recherche.length() > 0)
                  recherche = recherche.substring(0, 1).toUpperCase() + recherche.substring(1).toLowerCase();

                  ArrayList<User> results = new ArrayList<>();
                  for(User user : listUsers){
                  if(user.getName() != null && user.getName().contains(recherche)){
                  results.add(user);
                  }
                  }
                  updateListUsers(results);
                  }


                  Here i notify the the Adapter of the RecyclerView that the data changed:



                  private void updateListUsers(ArrayList<User> listUsers) {

                  // Sort the list by date
                  Collections.sort(listUsers, new Comparator<User>() {
                  @Override
                  public int compare(User o1, User o2) {
                  int res = -1;
                  if (o1.getDate() > (o2.getDate())) {
                  res = 1;
                  }
                  return res;
                  }
                  });

                  userRecyclerAdapter = new UserRecyclerAdapter(listUsers, InvitationActivity.this, this);
                  rvUsers.setNestedScrollingEnabled(false);
                  rvUsers.setAdapter(userRecyclerAdapter);
                  layoutManagerUser = new LinearLayoutManager(getApplicationContext());
                  rvUsers.setLayoutManager(layoutManagerUser);
                  userRecyclerAdapter.notifyDataSetChanged();
                  }


                  And of course the SearchView:



                   @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);

                  MenuItem search = menu.findItem(R.id.action_search);
                  SearchView searchView = (SearchView) search.getActionView();

                  searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

                  @Override
                  public boolean onQueryTextSubmit(String query) {
                  //Toast.makeText(MainActivity.this, "SEARCH " + query, Toast.LENGTH_LONG).show();
                  searchUsers(query);
                  return false;
                  }

                  @Override
                  public boolean onQueryTextChange(String newText) {
                  //Toast.makeText(MainActivity.this, "SEARCH " + newText, Toast.LENGTH_LONG).show();
                  searchUsers(newText);
                  return false;
                  }
                  });

                  return true;
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 1 at 13:54









                  Moussa DialloMoussa Diallo

                  12




                  12






























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53991448%2fhow-to-search-list-of-data-from-firebase-firestore-using-android-searchview%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

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas