Changing the background color of a view in real-time.

Posted by Tarmon on Stack Overflow See other posts from Stack Overflow or by Tarmon
Published on 2010-04-03T02:12:23Z Indexed on 2010/04/03 2:13 UTC
Read the original article Hit count: 404

Filed under:

Hey Everyone,

I am trying to implement a ListView that is composed of rows that contain a View on the left followed by a TextView to the right of that. I want to be able to change the background color of the first View based on it's position in the ListView. Below is what I have at this point but it doesn't seem to due anything.

public class Routes extends ListActivity {
    String[] ROUTES;
    TextView selection;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ROUTES = getResources().getStringArray(R.array.routes);

        setContentView(R.layout.routes);
        setListAdapter(new IconicAdapter());
        selection=(TextView)findViewById(R.id.selection);

    }

    public void onListItemClick(ListView parent, View v, int position, long id) {
        selection.setText(ROUTES[position]);

    }

    class IconicAdapter extends ArrayAdapter<String> {
        IconicAdapter() {
            super(Routes.this, R.layout.row, R.id.label, ROUTES);
        }
    }

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

        LayoutInflater inflater = getLayoutInflater();
        View row = inflater.inflate(R.layout.row, parent, false);
        TextView label = (TextView) row.findViewById(R.id.label);

        label.setText(ROUTES[position]);

        View icon = (View) row.findViewById(R.id.icon);

        switch(position){

        case 0:
            icon.setBackgroundColor(R.color.Red);
            break;
        case 1:
            icon.setBackgroundColor(R.color.Red);
            break;
        case 2:
            icon.setBackgroundColor(R.color.Green);
            break;
        case 3:
            icon.setBackgroundColor(R.color.Green);
            break;
        case 4:
            icon.setBackgroundColor(R.color.Blue);
            break;
        case 5:
            icon.setBackgroundColor(R.color.Blue);
            break;
        case 6:
            icon.setBackgroundColor(R.color.Gray);
            break;
        case 7:
            icon.setBackgroundColor(R.color.Yellow);
            break;
        case 8:
            icon.setBackgroundColor(R.color.Brown);
            break;
        case 9:
            icon.setBackgroundColor(R.color.Brown);
            break;
        case 10:
            icon.setBackgroundColor(R.color.Brown);
            break;
        case 11:
            icon.setBackgroundColor(R.color.Purple);
            break;
        case 12:
            icon.setBackgroundColor(R.color.Red);
            break;
        case 13:
            icon.setBackgroundColor(R.color.Gold);
            break;
        case 14:
            icon.setBackgroundColor(R.color.Orange);
            break;
        }

        return(row);
    }

}

Any input is appreciated and if you have any questions don't hesitate to ask!

Thanks, Rob

© Stack Overflow or respective owner

Related posts about android