首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >For cycle和if语句

For cycle和if语句
EN

Stack Overflow用户
提问于 2017-08-14 15:25:58
回答 1查看 33关注 0票数 0

伙计们!我有一个问题,对于cycle.So,我有一个.I(),它遍历数据库中第三列的每个值(应该是日期格式),如果添加日期的月份与当前的month.The问题相同,我想要更改列表视图中项目的背景颜色-如果我使用下面的代码:

代码语言:javascript
复制
public void setItemRed(View view){

    for(int i = 0 ; i <= myDB.getLastID() ; i++){
        String date = myDB.getcol3(i);
        String day = date.substring(0 , 2);
        String month = date.substring(3 , 5);
        String currentDate = currentDate();
        String currentMonth = currentDate.substring(3 , 5);

        listView.getChildAt(i).setBackgroundColor(Color.RED);
    }

} 

一切正常,当我添加if时,每一项都会显示红色的background.But:

代码语言:javascript
复制
public void setItemRed(View view){

    for(int i = 0 ; i <= myDB.getLastID() ; i++){
        String date = myDB.getcol3(i);
        String day = date.substring(0 , 2);
        String month = date.substring(3 , 5);
        String currentDate = currentDate();
        String currentMonth = currentDate.substring(3 , 5);
       if(date.length() == 10){
           if(month == currentMonth) {
               listView.getChildAt(i).setBackgroundColor(Color.RED);
           }
       }
    }

}

它不会提前work.Thank你!

EN

回答 1

Stack Overflow用户

发布于 2017-08-14 15:33:55

每当比较两组字符串时,都需要使用.equals()命令。简单地使month == currentMonth不起作用并返回false。

因此,请尝试用month.equals(currentMonth)替换month == currentMonth

希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45669406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档