-1

back with yet another android question and feel as if I am missing something simple here.

The viewholder gets the content in the specific field of the table from which the result is read.

Now I would like to compare that result to a char to change the color of the layout it is in but unfortunately is not working as planned.

Here is the code for the holder:

@Override
public void bindView(View view, Context context, Cursor cursor) {
    super.bindView(view, context, cursor);
    ViewHolder holder = (ViewHolder) view.getTag();
    if (holder == null) {
        holder = new ViewHolder();
        holder.colImp = cursor.getColumnIndexOrThrow(BetDbAdapter.COL_OUTCOME);
        holder.listTab = view.findViewById(R.id.won_lost);
        view.setTag(holder);
    }
    if (cursor.getString(holder.colImp).trim() == "W" || cursor.getString(holder.colImp).trim() == "w") {
        holder.listTab.setBackgroundColor(context.getResources().getColor(R.color.green));
    } else {
        holder.listTab.setBackgroundColor(context.getResources().getColor(R.color.red));
    }
}

The if statement just keeps returning the red even though the content is a "W", here is a screen of the app:

screen

Also this is the insert, it is hardcoded at the moment for testing purposes:

mDbAdapter.createBet("W", 12.34, 14.78);
mDbAdapter.createBet("W", 23.77, 1.90);
mDbAdapter.createBet("W", 123.45, 134);
mDbAdapter.createBet("W", 0.4, 1.34);

Please see if you can see what I cannot to fix this.

  • Use equalsIgnoreCase instead of == – Rajesh Jadav Oct 27 '15 at 13:44
  • Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Selvin Oct 27 '15 at 13:54
  • @Selvin I don't think so, I am asking whats wrong with my code, not how to do something, I am new to android and was unaware you cant use "==". I showed all my code so you can see I am trying, there was just a small mistake I couldn't find. –  Oct 27 '15 at 13:58
  • yes, it is ... it is also lack of java's basics ... How answering to such it will help for the future reader? – Selvin Oct 27 '15 at 14:00
  • Ok, understood, but i am not here to be criticized for not knowing something I am trying to learn. –  Oct 27 '15 at 14:02

3 Answers3

1

The problem is in

cursor.getString(holder.colImp).trim() == "W" || cursor.getString(holder.colImp).trim() == "w"

The == equality only works for primitive values, or to check if objects are the same reference. Try the following

cursor.getString(holder.colImp).trim().equalsIgnoreCase("W")
Gent Ahmeti
  • 1,559
  • 13
  • 17
0

String is an Object so equality comparator just test reference but not value.

To test String equality you should use

cursor.getString(holder.colImp).trim().equal("W");
Neige
  • 2,770
  • 2
  • 15
  • 21
0

You are comparing Strings in code, not chars. Char - 'W'. String - "W"

If you need compare chars:

cursor.getString(holder.colImp).trim().charAt(0) == 'W'

but easier to compare Strings

"W".equals(otherString);
m0rphine
  • 126
  • 4