繁体   English   中英

如何获取包含某个单词的行数?

[英]How to get the number of rows that contain a certain word?

所以我有这个代码生成的表,我一直在努力让这段代码工作,基本上我想知道有多少行有这个词,这样我以后就可以处理那个数量的元素,这段代码可能不起作用你们能看到错误吗? 代码:

public int UntilArraySearch (String filtro){
    int tope=0;
    for(int i = 0;i<mTableLayout.getChildCount();i++){
        if(data[i].productName.contains(filtro) || data[i].productCode.contains(filtro))
            tope++;
    }
    return tope;
}

并弹出此错误:

 Caused by: java.lang.ArrayIndexOutOfBoundsException: length=52; index=52

每当我运行该功能时,我都会收到此错误

只有当mTableLayout.getChildCount()<= data.length ,你的循环才会工作,而它似乎不是,因为如果是这种情况,那么i不会超过data的最大索引。

既然你想遍历data ,为什么不写这样的循环:

for(int i = 0;i<data.length;i++){
    if(data[i].productName.contains(filtro) || data[i].productCode.contains(filtro))
        tope++;
}

并检查这是否是预期的结果?

如果您正在搜索data ,那么您不妨遍历data数组的长度。 无论您的视图 ( mTableLayout ) 是否正确反映了模型 ( data ),此操作都可以通过仅考虑模型来更好地将自身与视图隔离开来,这是通常要遵循的良好做法。

public int UntilArraySearch (String filtro){
    int tope=0;
    for(int i = 0;i<data.length;i++){
        if(data[i].productName.contains(filtro) || data[i].productCode.contains(filtro))
            tope++;
    }
    return tope;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM