【问题标题】:Java if- else strings .contains not working errorJava if-else strings.contains not working 错误
【发布时间】:2017-11-13 06:06:29
【问题描述】:

如果我输入entity.about = "phone"test = "phone",则搜索列表会更新,但如果我输入entity.about = "this is phone"test = "phone",则搜索列表不会更新。

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) ;
                }
            }
        }
    }
}

【问题讨论】:

  • 您能否发布一个可重现的问题示例?你所描述的不应该发生。
  • 尝试打印 entity.about 并在 if 条件之前测试值,并在 if 执行之前检查实际值。
  • 我找到了。这是因为我在搜索之前没有添加 .tolowercase。
  • @RahulMishra 发帖回答并接受你的回答。

标签: java android string firebase if-statement


【解决方案1】:

将代码:entity.about.contains(test) 替换为 entity.about.toLowerCase.contains(test)

【讨论】:

    【解决方案2】:

    没有考虑到 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) ;
                    }
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-24
      • 2018-09-11
      • 2020-04-04
      • 2017-08-09
      • 2021-05-29
      • 2022-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多