Java if- else strings .contains not working error

时间:2017-11-13 06:06:30

标签: java android string firebase if-statement

If I put entity.about = "phone" and test = "phone" then the search list is updated but if I put entity.about = "this is phone" and test = "phone" then the search list is not updated.

public void onSearchConfirmed(final CharSequence text) {
    productsRef = FirebaseDatabase.getInstance().getReference("products");
    searchList.clear();
    final String test = text.toString();
    productsRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                ProductEntity entity = postSnapshot.getValue(ProductEntity.class);
                if(entity.about.contains(test) || entity.vendor_address.contains(test)) {
                    searchList.add(entity) ;
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

Replace code:entity.about.contains(test) with entity.about.toLowerCase.contains(test)

答案 1 :(得分:0)

我没做错,不考虑String类中包含函数的事实实际上是区分大小写的。这就是我需要做的事情:

final String test = text.toString().toLowerCase() ;
    productsRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                ProductEntity entity = postSnapshot.getValue(ProductEntity.class);
                if(entity.about.toLowerCase().contains(test) || entity.vendor_address.toLowerCase().contains(test)) {
                    searchList.add(entity) ;
                }
            }
相关问题