0

guys! I am having a problem with for cycle.So i have a for() that goes through every value of third column from database(which is supposed to be in date format).I want to change the background color of an item from listview if the month of the added date is the same as the current month.The problem is there - if i use the code like this:

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

} 

Everything works and every item gets red background.But when i add if :

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

}

It does not work.Thank you in advance!

Goshoy
  • 1
  • 4
  • 1
    Try sorting out your indentation on the `if statement`. –  Aug 14 '17 at 07:28
  • `month == currentMonth` will always return false. Use `month.eqauals(currentMonth)` instead – mihail Aug 14 '17 at 07:29
  • Thank you mihail! Solved!But what is important is why does it return always false? – Goshoy Aug 14 '17 at 07:32
  • read some java basics first. And learn to do a research on you own before asking basic questions: https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – mihail Aug 14 '17 at 07:33
  • Well i am a newbie still.Anyways,Thank you! – Goshoy Aug 14 '17 at 07:35
  • @Goshoy I understand that, but being a newbie doesn't stop you from researching. Spend a little more time finding out the problem on your own is much better. – mihail Aug 14 '17 at 07:43

1 Answers1

0

Whenever comparing two sets of string, you need to use the .equals() command. Simply by making month == currentMonth will not work and return false.

So try replacing month == currentMonth with month.equals(currentMonth).

Hope that helps.