For cycle and if statement

168
August 14, 2017, at 6:15 PM

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!

Answer 1

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.

READ ALSO
Android scaling copy of image instead of changing source image

Android scaling copy of image instead of changing source image

I have the following code for getting small sized avatar image:

235
Android WebView with an embedded iframe YouTube and Facebook videos fullscreen

Android WebView with an embedded iframe YouTube and Facebook videos fullscreen

I am struggling with some issues in iframe Videos in Android webview, Can you please help me

496
how add a toolbar with material searchview inside a fragment?

how add a toolbar with material searchview inside a fragment?

i am using https://githubcom/MiguelCatalan/MaterialSearchView as my searchView with toolbar

233