我有一个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*/
}
答案 0 :(得分:3)
我在commonsware.com的“忙碌编码器Android开发指南”的免费摘录中找到了一个很好的教程。另请在youtube上查看Google I/O 2010 - The world of ListView,其中包含大量有用的信息。
基本上我必须做的就是创建自定义ArrayAdapter并覆盖getView()。查看下面的代码。
public class Lwm extends ListActivity {
private TextView mSelection;
private List<MonitorObject> 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<MonitorObject> {
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<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;
}
}