Reputation: 6063
I have this 3 lines of code which I use to hold the state of items in my listview when scrolling, but my problem is, the last line of code only gets executed. This is the code block:
holder.viewName.setTextColor((priority.equals("Low")? Color.BLUE: Color.GRAY ) );
holder.viewName.setTextColor((priority.equals("Medium")? Color.GREEN: Color.GRAY ) );
holder.viewName.setTextColor((priority.equals("High")? Color.RED: Color.GRAY ) ); // items in this state condition only gets executed in the listview, the rest are ignored and set to gray.
Is there a way I can join the code logic together, so that all 3 conditions can be called?
Upvotes: 2
Views: 250
Reputation: 19344
first I would use equalsIgnoreCase because there is nothing more frustrating than losing hours looking for a case sensitive error ^^
You problem is that your code is limited to the last statement
holder.viewName.setTextColor((priority.equals("High")? Color.RED: Color.GRAY ) );
what happens when the case of low or medium is that they are set at the right color then arrives the test of high that switches it back to gray
you want to change you code to a switch or else if statement.
color = Color.GRAY;
if (priority.equals("Low")) {
color = Color.BLUE;
} else if (priority.equals("Medium")) {
color = Color.GREEN;
} else if (priority.equals("High")) {
color = Color.RED;
}
holder.viewName.setTextColor(color);
Finally the you don't need the ternair here (condition)?val1:val2
it is not a bad idea with 2 colors, but with 4 it looses interst
Upvotes: 1
Reputation: 52229
All 3 lines get executed, but you're always overriding the text color with the next line. Use a condition instead:
if (priority.equals("Low")) {
holder.viewName.setTextColor(Color.BLUE);
} else if (priority.equals("Medium")) {
holder.vieName.setTextColor(Color.GREEN);
} else if (priority.equals("High")) {
holder.viewName.setTextCOlor(Color.RED);
} else {
holder.viewName.setTextColor(Color.GRAY);
}
Edit: Switch statements for strings is not yet availalbe in Java right?
Upvotes: 3