2017-08-14 5 views
0

人!私はcycle.Soに問題があります。私はfor()データベースからの3番目の列のすべての値を通過する(これは、日付形式であることになっています)。私はlistviewからアイテムの背景色を変更したい現在month.Theの問題があるとして追加日付の月は同じである - 私はこのようなコードを使用する場合:サイクルと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); 

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

} 

をすべてが動作し、私があれば追加したときに、すべての項目が赤background.Butを取得します。

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

} 

これは動作しません。事前におねがいします!

+1

あなたのインデントをif文でソートしてみてください。 –

+0

'month == currentMonth'は常にfalseを返します。代わりに 'month.eqauals(currentMonth)'を使用してください – mihail

+0

mihailありがとう!しかし、重要なのはなぜそれが常に偽に戻るのかということです。 – Goshoy

答えて

0

2組の文字列を比較するときは、.equals()コマンドを使用する必要があります。単純にmonth == currentMonthを作ってもうまくいかず、falseを返します。

month == currentMonthmonth.equals(currentMonth)に置き換えてください。

希望に役立ちます。