如何让我的搜索功能在安卓Java中允许小写,或忽略大小写敏感性。

huangapple go评论78阅读模式
英文:

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(&quot;&quot;);
        }
    }
});

and my searchInDB function:

private void searchInDB(final String s) {
    firebaseFirestore.collection(&quot;Shops&quot;).orderBy(&quot;name&quot;).startAt(s).endAt(s+&quot;\uf8ff&quot;).get().addOnCompleteListener(new OnCompleteListener&lt;QuerySnapshot&gt;() {
        @Override
        public void onComplete(@NonNull Task&lt;QuerySnapshot&gt; 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(&quot;&quot;, &quot;Error&quot;);
            }
        }
    });
}

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.

huangapple
  • 本文由 发表于 2020年8月26日 07:11:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63588323.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定