I need to change background color of list view dynamically based on string value (JSON). I want to change the background color if the text in the TAG_SEVERITY field is "critical". Below is how I tried, but With code it changes the severity field to blue regardless of the text. I t would be etter if the row color is changed instead of field background color. I'm quite new to this.
SimpleAdapter adapter = new SimpleAdapter(this, mCommentList,
R.layout.single_post, new String[] {TAG_SEVERITY, TAG_TITLE }, new int[] { R.id.severity, R.id.rTitle});adapter.setViewBinder(new SimpleAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Object data,
String textRepresentation) {
int v=view.getId();
if(v==R.id.severity && String.valueOf(data.toString()).contentEquals("Critical")){
((View) view.getParent()).setBackgroundColor(Color.BLUE);
}
TextView TV=(TextView) view;
TV.setText(data.toString());
return true;
}
});
setListAdapter(adapter);
I think that by setting the background for the view.getParent(), you are setting the background for the entire ListView. Have you tried using just view.setBackground()?
The other alternative is to create your own adapter class, and setting the view's background color in the getView() method based on the severity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With