2010-11-09 2 views
1

J'ai une ListActivity affichant un tas d'objets d'une liste. Je veux changer le fond & la couleur du texte de la ligne en fonction de l'état des deux booléens dans le MonitorObject.Android ListActivity couleur de la ligne basée sur l'état de l'objet

Dois-je étendre ArrayAdapter? Si oui, un échantillon de code serait grandement apprécié car j'ai essayé de le comprendre pendant quelques jours sans succès.

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

Répondre

3

J'ai trouvé un tutoriel sur la façon de le faire dans l'extrait gratuit pour 'Guide The Busy Coder pour Android Development' à commonsware.com. Consultez également Google I/O 2010 - The world of ListView sur youtube, il contient beaucoup d'informations utiles.

Fondamentalement, ce que j'ai dû faire était de créer un ArrayAdapter personnalisé et de surcharger getView(). Découvrez le code ci-dessous.

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