Please help solve this listview problem
i have this 3 lines of code which i use to hold the state of items in my listview wjen scrolling, but my problem i开发者_开发知识库s, the last line of code only gets executed. could you please help me solve this. 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?.. thank you
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?
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
精彩评论