Mou*_*llo 5 android firebase google-cloud-firestore
我使用 Firebase 的 Cloud Firestore 来存储用户和 android Searchview 来提供搜索功能。例如,当用户搜索“jonathan”时,如果他开始输入“j”,我想引入名称以“j”开头的所有用户。我怎样才能做到这一点?
这是我尝试过的:
@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;
}
Run Code Online (Sandbox Code Playgroud)
从 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);
}
});
}
Run Code Online (Sandbox Code Playgroud)
这仅适用于第一个字母“j”,例如我添加“ja”后,我仍然显示所有“jonathan”
如果找到解决方案,感谢@Oby 的建议。事实上,我并不需要每次触发搜索时都查询数据库,因为我有用户列表。我只需在列表中进行搜索,如下所示:我们首先获取列表:
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);
}
});
}
Run Code Online (Sandbox Code Playgroud)
这是搜索功能:
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);
}
Run Code Online (Sandbox Code Playgroud)
这里我通知RecyclerView的Adapter数据发生了变化:
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();
}
Run Code Online (Sandbox Code Playgroud)
当然还有 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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5711 次 |
| 最近记录: |