How to filter Recycleview by ignoring case?

问题: I am getting data from Recyclerview I am adding the searchview filter in my app, but when I search capital and lower text the no result found I think may be causing a probl...

问题:

I am getting data from Recyclerview I am adding the searchview filter in my app, but when I search capital and lower text the no result found I think may be causing a problem on the string, please help me with this!!

private void search(final String s){
            final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
            Query query = FirebaseDatabase.getInstance().getReference("Users").orderByChild("name").startAt(s).endAt(s + "uf8ff");


        query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                if (!TextUtils.isEmpty(s)) {
                    arrayList.clear();
                    for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                        final Contacts contacts = dataSnapshot1.getValue(Contacts.class);
                        assert firebaseUser != null;
                        assert contacts != null;
                        if (!contacts.getUid().equals(firebaseUser.getUid())) {
                            arrayList.add(contacts);
                        }
                    }


                 MyAdapter myAdapter = new MyAdapter(getApplication(), arrayList, new MyAdapter.OnItemClickListener() {
                  @Override
                   public void onItemClick(Contacts item) {
                            //Toast.makeText(FindFriendsActivity.this, "Item Clicked", Toast.LENGTH_LONG).show();
                    String visit_user_id=item.getUid();
                    Intent profileIntent=new Intent(FindFriendsActivity.this,ProfileActivity.class);
                     profileIntent.putExtra("visit_user_id",visit_user_id);
                      startActivity(profileIntent);
                        }
                    });

                    FindFriendsRecyclerList.setAdapter(myAdapter);
                    myAdapter.notifyDataSetChanged();

                } else {
                    arrayList.clear();
                    //onStart();
                    FindFriendsRecyclerList.setLayoutManager(new LinearLayoutManager(FindFriendsActivity.this));
                    adapter.startListening();
                    FindFriendsRecyclerList.setAdapter(adapter);
                }

            }
            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }

I get the from query and firebase data snapshot but when I search capital and lower text on searchview the no result found how can I ignore the case problem in my code


回答1:

You can use equals Ignore case for that...this is the string function for ignoring case.

if (!contacts.getName().toLowerCase().contains(s.toString().toLowerCase())) // something like this..
{
    arrayList.add(contacts);
}

You can try with .toLowerCase() on your username and the text you are getting from search view and check.... for searching on username...

I consider that you might be getting all the data in your contacts class's object.. then you just have to check the entered string from search view in your get filter method like above and .contains will check if entered string is present or not... if you want you can also do .startsWith(s.toString().toLowerCase()) if you want to search the name starting with the string entered in the search view..


回答2:

public boolean regionMatches(boolean ignoreCase,
                             int toffset,
                             String other,
                             int ooffset,
                             int len)

ignoreCase : if true, ignores case when comparing characters.

public static boolean containsIgnoreCase(String str, String searchStr)     {
        if(str == null || searchStr == null) return false;

        final int length = searchStr.length();
        if (length == 0)
            return true;

        for (int i = str.length() - length; i >= 0; i--) {
            if (str.regionMatches(true, i, searchStr, 0, length))
                return true;
        }
        return false;
    }

回答3:

You can also convert both of them to same case using toLowerCase() or toUpperCase():

 if (!contacts.getUid().toLowerCase().equals(firebaseUser.getUid().toLowerCase())) {
     arrayList.add(contacts);
 }
  • 发表于 2019-01-18 18:54
  • 阅读 ( 202 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除