我有一个 ListActivity 显示列表中的一堆对象。我想根据 MonitorObject 中两个布尔值的状态更改行的背景和文本颜色。
我需要扩展 ArrayAdapter 吗?如果是这样,将非常感谢代码示例,因为我已经尝试了几天但没有成功。
public class Lwm extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
setListAdapter(new ArrayAdapter<MonitorObject>(this, R.layout.row, getMonitorObjects()));
}
private List<MonitorObject> getMonitorObjects() {
List<MonitorObject> mos = new ArrayList<MonitorObject>();
mos.add(new MonitorObject(15000, 20000, 25000));
mos.add(new MonitorObject(15000, 14000, 18000));
mos.add(new MonitorObject(15000, 12000, 14000));
mos.add(new MonitorObject(100, 200, 250));
mos.add(new MonitorObject(3000, 2500, 3500));
return mos;
}
}
public class MonitorObject {
private int mTimeTotal;
private int mWarningThreshold;
private int mAlarmThreshold;`enter code here`
private boolean mWarning;
private boolean mAlarm;
public MonitorObject(int timeTotal, int warningThreshold, int alarmThreshold) {
this.mTimeTotal = timeTotal;
this.mWarningThreshold = warningThreshold;
this.mAlarmThreshold = alarmThreshold;
mWarning = (mTimeTotal > mWarningThreshold) ? true : false;
mAlarm = (mTimeTotal > mAlarmThreshold) ? true : false;
}
/*getters, setters, tostring goes here*/
}