我有一个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*/}
解决方法:
我在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; }}
总结 以上是内存溢出为你收集整理的java-Android ListActivity基于对象状态的行颜色全部内容,希望文章能够帮你解决java-Android ListActivity基于对象状态的行颜色所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)