我需要根據字符串值(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);
你有沒有試過設置一個斷點,看看它爲什麼進入if語句時,你認爲它不應該?由於某種原因,字符串的值總是至關重要的? –
我已經查過Json字符串,它並不總是「關鍵」 – user2602159
但是if語句在執行時有什麼斷點呢? –