0

Ok so far I have created this, and my items are being displayed. Lets suppose that I am having the key values in an array with as many elements as the rows. If I have 16 rows, then my array has 16 elements. I want to check the array[0] and depending on the value setColor to first row, then check array[1] and setColor to next row and so on. Can anyone help me:

public class SimpleAdapter extends ArrayAdapter<String>{

private final Context context;



private String data[] = null;

    public SimpleAdapter(Context context,  String[] data) {
        super(context, R.layout.row, data);
        this.context = context;
        this.data = data;
    }



  @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.row, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.text1);
        textView.setText(data[position]);
        return rowView;
    }
}
ghostrider
  • 5,131
  • 14
  • 72
  • 120
  • do you want to change the background color of the Item in the the Listview or you want to change the setTextColor for the TextView there? – Sergey Benner Jan 13 '12 at 15:49
  • possible duplicate? http://stackoverflow.com/questions/2217753/changing-background-color-of-listview-items-on-android – Sergey Benner Jan 13 '12 at 15:53
  • setText Color. it's not exact duplicate, I have read it but the problem is I do not know how to pass the parameters I want to the Adapter (meaning the array) – ghostrider Jan 13 '12 at 16:14

2 Answers2

0

In getView() you can have something like:

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.row, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.text1);
textView.setText(data[position]);

switch(position) {
  case 0:
   rowView.setBackgroundColor(color0)
   break;
  case 1:
   rowView.setBackgroundColor(color1)
   break;
...
}

return rowView;
Daniel
  • 984
  • 8
  • 14
0

It is simple define your color/(background drawable) in an array. like

int[] color = {your colors };

then just use the below lines

int mycolor = position % color.length;
rowView.setBackground/color = color[mycolor];
TextView textView = (TextView) rowView.findViewById(R.id.text1);
textView.setText(data[position]);
return rowView;
Sunny
  • 14,522
  • 15
  • 84
  • 129