我需要根据字符串值(JSON)动态更改列表视图的背景颜色。如果TAG_SEVERITY字段中的文本为“critical”,则要更改背景颜色。以下是我的尝试方式,但是使用代码时它会将严重性字段更改为蓝色,而不考虑文本内容。最好更改行颜色而不是字段背景颜色。我对此还很陌生。
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);