Reputation: 33
I'm writing an application that checks our 'webservice' and returns a 'Managed' or 'Unmanaged' status, I keep a running search history in a listview (until the user clears it), this is all working.
What I'd like to do is, if the status is 'Managed' I'd like the single textview item to be green and if it's 'Unmanaged' I'd like the single item to be red.
//This is the String Array which gets populated later.
ArrayList<String> _searchHistoryList = new ArrayList<String>();
//Here is where I override the getView to try to change the color.
listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, _searchHistoryList) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = (TextView) super.getView(position, convertView, parent);
String _currentStatus = appSettings.getString("current_status", "na");
int textColor;
if (_currentStatus.equals("Managed")){
textColor = R.color.green_text;
} else {
textColor = R.color.red_text;
}
textView.setTextColor(getResources().getColor(textColor));
return textView;
}
};
lvSearchHistory.setAdapter(listAdapter);
The above code actually turns the entire listview to the color of the last search item. (ex. if the last search results in a 'Managed' result - the entire list of items turns green, rather than just the single item).
Can anyone point me in the right direction.
Upvotes: 3
Views: 544
Reputation: 1914
Try the following code
if (_currentStatus.equals("Managed")){
textView.setTextColor(0xFF008B45);
} else {
textView.setTextColor(0xFFB0171F);
}
Upvotes: 1
Reputation: 3412
try the following code:
int textColor;
if (_currentStatus.equals("Managed")){
textColor = R.color.green_text;
textView.setTextColor(getResources().getColor(textColor));
} else {
textColor = R.color.red_text;
textView.setTextColor(getResources().getColor(textColor));
}
return textView;
Upvotes: 0
Reputation: 1910
Looks to me like the source of your problem is this call:
String _currentStatus = appSettings.getString("current_status", "na");
There's nothing in this call that indicates which list element you're checking, so it'll always return the same result for each view in the list. You should pass in the position parameter and use that to get the status for the relevant list entry (how you do this depends on how you actually get the status for your list elements, but it shouldn't be too difficult).
Upvotes: 2