Android ListActivity基于对象状态的行颜色

我有一个ListActivity显示列表中的一堆对象。 我想根据MonitorObject中两个布尔值的状态更改行的背景和文本颜色。

我需要扩展ArrayAdapter吗? 如果是这样的话,代码样本将会非常受欢迎,因为我一直试图弄清楚它几天没有成功。

public class Lwm extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.list); setListAdapter(new ArrayAdapter(this, R.layout.row, getMonitorObjects())); } private List getMonitorObjects() { List mos = new ArrayList(); 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*/ } 

我在commonsware.com的“忙碌编码器Android开发指南”的免费摘录中找到了一个很好的教程。 另请查看Google I / O 2010 – youtube 上的ListView世界 ,它包含许多有用的信息。

基本上我必须做的就是创建一个自定义的ArrayAdapter并覆盖getView()。 查看下面的代码。

 public class Lwm extends ListActivity { private TextView mSelection; private List mMonitorObjects; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mMonitorObjects = getMonitorObjects(); setContentView(R.layout.main); setListAdapter(new CustomAdapter()); mSelection = (TextView)findViewById(R.id.selection); } @Override public void onListItemClick(ListView parent, View v, int position, long id){ mSelection.setText("Selection length is: " + mMonitorObjects.get(position).toString().length()); } private class CustomAdapter extends ArrayAdapter { CustomAdapter() { super(Lwm.this, R.layout.row, R.id.label, mMonitorObjects); } @Override public View getView(int position, View convertView, ViewGroup parent) { View row = convertView; if (row == null) { // This gives us a View object back which, in reality, is our LinearLayout with // an ImageView and a TextView, just as R.layout.row specifies. LayoutInflater inflater = getLayoutInflater(); row = inflater.inflate(R.layout.row, parent, false); } TextView label = (TextView) row.findViewById(R.id.label); label.setText(mMonitorObjects.get(position).toString()); ImageView icon = (ImageView)row.findViewById(R.id.icon); MonitorObject mo = getMonitorObjects().get(position); if (mo.ismAlarm()) { icon.setImageResource(R.drawable.alarm); row.setBackgroundColor(Color.RED); } else if (mo.ismWarning()){ icon.setImageResource(R.drawable.warning); row.setBackgroundColor(Color.YELLOW); } else { icon.setImageResource(R.drawable.ok); row.setBackgroundColor(Color.GREEN); } return row; } } private List getMonitorObjects() { List mos = new ArrayList(); 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; } }