gpt4 book ai didi

java - Android ListActivity 行颜色基于对象状态

转载 作者:行者123 更新时间:2023-11-29 06:18:31 25 4
gpt4 key购买 nike

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

我需要扩展 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 上的“The Busy Coder's Guide to Android Development”的免费摘录中找到了关于如何执行此操作的精彩教程。另请查看 Google I/O 2010 - The world of ListView在 youtube 上,它包含很多有用的信息。

基本上,我必须做的就是创建自定义 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 行颜色基于对象状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4134875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com