在自定义适配器android中着色一行

时间:2012-01-13 15:44:42

标签: android adapter custom-adapter

好了到目前为止我创建了这个,我的项目正在显示。让我们假设我在一个数组中的键值与行数一样多。如果我有16行,那么我的数组有16个元素。我想检查数组[0]并根据值setColor到第一行,然后检查数组[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;
    }
}

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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;