英文:
how to let my search function allow lowercase, or ignore case sensitivity in android java
问题
我创建了一个搜索功能,一切都正常工作,只是当输入小写字母时,它无法检索任何值,但当输入大写字母时却可以。
我如何使它不区分大小写?
我的搜索文本更改监听器:
searchShops.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable s) {
if (!s.toString().isEmpty()) {
searchInDB(s.toString());
} else {
searchInDB("");
}
}
});
以及我的searchInDB
函数:
private void searchInDB(final String s) {
firebaseFirestore.collection("Shops").orderBy("name").startAt(s).endAt(s + "\uf8ff").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
QuerySnapshot querySnapshot = task.getResult();
arrayList.clear();
for(QueryDocumentSnapshot DocumentSnapshot : querySnapshot) {
final Shop shop = DocumentSnapshot.toObject(Shop.class);
arrayList.add(shop);
}
SearchAdapter searchAdapter = new SearchAdapter(getApplicationContext(),arrayList);
recyclerView.setAdapter(searchAdapter);
searchAdapter.notifyDataSetChanged();
} else {
Log.d("", "Error");
}
}
});
}
我如何修复这个问题?
英文:
i created a search functionality and everything works perfectly except that when the letter is lower case it doesn't retrieve any values but when it is uppercase it does.
how do i make it case insensitive?
My search text change listener:
searchShops.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable s) {
if (!s.toString().isEmpty()) {
searchInDB(s.toString());
} else {
searchInDB("");
}
}
});
and my searchInDB function:
private void searchInDB(final String s) {
firebaseFirestore.collection("Shops").orderBy("name").startAt(s).endAt(s+"\uf8ff").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
QuerySnapshot querySnapshot = task.getResult();
arrayList.clear();
for(QueryDocumentSnapshot DocumentSnapshot : querySnapshot) {
final Shop shop = DocumentSnapshot.toObject(Shop.class);
arrayList.add(shop);
}
SearchAdapter searchAdapter = new SearchAdapter(getApplicationContext(),arrayList);
recyclerView.setAdapter(searchAdapter);
searchAdapter.notifyDataSetChanged();
} else {
Log.d("", "Error");
}
}
});
}
how do i fix this?
答案1
得分: 0
Firestore没有不区分大小写的搜索功能。它只能匹配完全相同的字符串,或者在您特定的情况下,匹配"name"字段的字符串前缀。
通常人们为了解决这个问题会添加另一个字段来进行查询。您可以添加一个名为"nameSearch"的新字段,并确保:
- nameSearch始终是name的小写版本。
- 客户端只使用完全小写的字符串值来查询nameSearch字段。
英文:
Firestore does not have case-insensitive searches. It will only match exact strings, or in your specific case, exact string prefixes for values of the "name" field.
Usually what people do to work around this is add another field just for querying. You could add a new field called "nameSearch" and make sure that:
- nameSearch is always a lowercase version of name.
- nameSearch is only queried by the client with fully lowercase string values.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论