小编典典

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

java

我有一个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*/
}

阅读 220

收藏
2020-11-23

共1个答案

小编典典

我在commonsware.com的“ Android开发的繁忙编码员指南”的免费摘录中找到了有关如何执行此操作的出色教程。还可以在youtube
查看Google I / O
2010-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;
  }
}
2020-11-23