0

好的,到目前为止我已经创建了这个,并且我的项目正在显示。假设我在一个数组中拥有与行一样多的元素的键值。如果我有 16 行,那么我的数组有 16 个元素。我想检查 array[0] 并根据值 setColor 到第一行,然后检查 array[1] 和 setColor 到下一行,依此类推。谁能帮我:

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

2 回答 2

0

在 getView() 你可以有类似的东西:

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;
于 2012-01-13T15:56:27.337 回答
0

color/(background drawable)在数组中定义你的很简单。喜欢

int[] color = {your colors };

然后只需使用以下几行

int mycolor = position % color.length;
rowView.setBackground/color = color[mycolor];
TextView textView = (TextView) rowView.findViewById(R.id.text1);
textView.setText(data[position]);
return rowView;
于 2012-01-13T15:57:00.773 回答