基于对象状态的Andr​​oid ListActivity排颜色对象、颜色、状态、Andr

2023-09-07 09:47:12 作者:彼岸回眸丶你许来一世温暖

我有一个列表显示了一堆对象的ListActivity。我想改变的背景和放大器;该行的基础上,在MonitorObject两个布尔值的状态文本颜色。

我需要扩展ArrayAdapter?如果是这样,一个code样品将大大pciated,因为我一直在试图弄明白了,但没有成功过几天AP $ P $。

 公共类LWM扩展ListActivity {
  @覆盖
  公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.list);
    setListAdapter(新ArrayAdapter&其中; MonitorObject>(此,R.layout.row,getMonitorObjects()));
  }

  私人列表< MonitorObject> getMonitorObjects(){
    名单< MonitorObject> MOS =新的ArrayList< MonitorObject>();
    mos.add(新MonitorObject(15000,20000,25000));
    mos.add(新MonitorObject(15000,14000,18000));
    mos.add(新MonitorObject(15000,12000,14000));
    mos.add(新MonitorObject(100,200,250));
    mos.add(新MonitorObject(3000,2500,3500));
    返回马鞍山;
  }
}
 

 公共类MonitorObject {
  私人诠释mTimeTotal;
  私人诠释mWarningThreshold;
  私人诠释mAlarmThreshold;`输入code here`
  私人布尔mWarning;
  私人布尔mAlarm;

  公共MonitorObject(INT timeTotal,诠释warningThreshold,诠释alarmThreshold){
    this.mTimeTotal = timeTotal;
    this.mWarningThreshold = warningThreshold;
    this.mAlarmThreshold = alarmThreshold;
    mWarning =(mTimeTotal> mWarningThreshold)?真假;
    mAlarm =(mTimeTotal> mAlarmThreshold)?真假;
  }
  / *的getter,setter方法​​,的toString到这里* /
}
 

解决方案

我发现如何做到这一点在commonsware.com免费摘录的忙碌codeR指南Android开发一个伟大的教程。另外,请查阅谷歌I / O 2010 - ListView的在YouTube上的世界,它含有大量的有用的信息。

基本上我不得不这样做赤身创建自定义ArrayAdapter并重写getView()。退房code波纹管。

 公共类LWM扩展ListActivity {
  私人TextView的mSelection;
  私人列表< MonitorObject> mMonitorObjects;

  @覆盖
  公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    mMonitorObjects = getMonitorObjects();
    的setContentView(R.layout.main);
    setListAdapter(新CustomAdapter());
    mSelection =(TextView中)findViewById(R.id.selection);
  }

  @覆盖
  公共无效onListItemClick(ListView的父,视图V,INT位置,长的id){
    mSelection.setText(选择长度为:+ mMonitorObjects.get(位置)的ToString()长度());
  }

  私有类CustomAdapter扩展ArrayAdapter< MonitorObject> {
    CustomAdapter(){
      超(Lwm.this,R.layout.row,R.id.label,mMonitorObjects);
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
      查看排= convertView;

      如果(行== NULL){
        //这给了我们一个视图对象返回其在现实中,我们的LinearLayout与
        //一个ImageView的和一个TextView,就像R.layout.row规定。
        LayoutInflater充气= getLayoutInflater();
        行= inflater.inflate(R.layout.row,父母,假);
      }

      TextView的标签=(TextView中)row.findViewById(R.id.label);
      label.setText(mMonitorObjects.get(位置)的ToString());
      ImageView的图标=(ImageView的)row.findViewById(R.id.icon);

      。MonitorObject月= getMonitorObjects()得到(位置);

      如果(mo.ismAlarm()){
        icon.setImageResource(R.drawable.alarm);
        row.setBackgroundColor(Color.RED);
      }否则如果(mo.ismWarning()){
        icon.setImageResource(R.drawable.warning);
        row.setBackgroundColor(Color.YELLOW);
      } 其他 {
        icon.setImageResource(R.drawable.ok);
        row.setBackgroundColor(Color.GREEN);
      }

      返回行;
    }
  }

  私人列表< MonitorObject> getMonitorObjects(){
    名单< MonitorObject> MOS =新的ArrayList< MonitorObject>();
    mos.add(新MonitorObject(15000,20000,25000));
    mos.add(新MonitorObject(15000,14000,18000));
    mos.add(新MonitorObject(15000,12000,14000));
    mos.add(新MonitorObject(100,200,250));
    mos.add(新MonitorObject(3000,2500,3500));
    返回马鞍山;
  }
}
 

I have a ListActivity displaying a bunch of objects from a list. I want to change the background & text color of the row based on the state of the two booleans in the MonitorObject.

Do I need to extend ArrayAdapter? If so, a code sample would be greatly appreciated as I have been trying to figure it out for a few days with no success.

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*/
}

解决方案

I found a great tutorial on how to do this in the free excerpt for 'The Busy Coder's Guide to Android Development' at commonsware.com. Also check out Google I/O 2010 - The world of ListView at youtube, it contains lots of useful information.

Basically what I had to do wast to create a custom ArrayAdapter and override getView(). Check out the code bellow.

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;
  }
}