views:

388

answers:

4

Hi. I am trying to change the color on a specific row depending on different states. This is the code i have at the moment.

 public View getView(int position, View convertView, ViewGroup parent) {

  View row=convertView;

     if (row==null) {                                                    
         LayoutInflater inflater=getLayoutInflater();

         row=inflater.inflate(R.layout.doit, parent, false);
     }

     TextView label = (TextView) row.findViewById(R.id.mess);

     label.setText(ArrayAdapter.getItem(position));

     switch(mState){
     case STATE1:

      label.setTextColor(Color.WHITE);
      break;
     case STATE2:
      label.setTextColor(Color.BLACK);
      break;
     case STATE3:
      label.setTextColor(Color.YELLOW);
      break;
     }


     return(row);
 }

}

The code kinda works..but it changes all the rows. Any ideas?

+2  A: 

so android reuses the View each time that's why you're seeing it affect all the rows. What you need to do is explicitly set the color for each case. perhaps add a 'default' case to your switch statement so that it sets it to whatever you're default is in the layout?

Ben
hi! tanks for the answear. I am not sure how i can do this. i understand that changing the rows colors explicitly will solve the problem...but i have no idea how to do it.
if you add a 'default:' case to your switch statement and call label.setTextColor() in there it should do the trick for you. basically what i mean is that, if you are going to do things that only affect certain rows, then you need to handle the other rows as well.i.e. instead of doing this:`if(position == MY_SPECIAL_ROW) { //setTextColor()}` you should do instead do this:`if(position == MY_SPECIAL_ROW) { //setTextColor()} else { //setDefaultTextColor();}
Ben
A: 

I am also wondering about this one. I havent found a way to style specific rows yet.

James Ford
A: 

Found some weird things about a ArrayAdapter. The getView() method is getting called more than once when you add something to the adapter. It's getting called for each item in the ArrayAdapter which is weird. This is why the case-switch does not work. When it iterates through the whole list it will still be in the same state. The solution is to find your special rows like Ben suggested. Like:

if (position == 2){ //Row 3 will be red
label.setTextColor(Color.RED)
}

I find this weird, but maybe it is how they have implemented it.

James Ford
This is really bad advice. Never hard code your colors to positions in the list.
chubbard
+1  A: 

Where is mState coming from? I don't see it coming from the object so how is it going to change value? If it can't change value as getView() is called for each row then the color can't change. I'd expect something like the following:

MyItem item = getItem( position );
switch( item.getState() ) {
   case STATE_1:
      label.setTextColor( R.color.white );
      break;
   case STATE_2:
      label.setTextColor( R.color.red );
      break;
   case STATE_3:
      label.setTextColor( R.color.green );
      break;
   default:
      label.setTextColor( R.color.black );
      break;
 }

Remember what Ben stated you have to reset the colors so if your row can't be in 1, 2, or 3 state you need to add a default branch to your switch statement.

It's generally good practice to get the information used to make rendering decisions from the row object.

chubbard