其实很简单,就是重载SimpleAdapter的getView函数。代码如下:
public class AltColorAdapter extends SimpleAdapter {
private static int[] mColors = { R.drawable.grey, R.drawable.white };
public AltColorAdapter(Context context,
List<? extends Map<String, ?>> data, int resource, String[] from,
int[] to) {
super(context, data, resource, from, to);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
int[] arrayOfInt = mColors;
int colorLength = mColors.length;
int selected = arrayOfInt[position % colorLength];
View localView = super.getView(position, convertView, parent);
localView.setBackgroundResource(selected);
return localView;
}
}
private static int[] mColors = { R.drawable.grey, R.drawable.white };
public AltColorAdapter(Context context,
List<? extends Map<String, ?>> data, int resource, String[] from,
int[] to) {
super(context, data, resource, from, to);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
int[] arrayOfInt = mColors;
int colorLength = mColors.length;
int selected = arrayOfInt[position % colorLength];
View localView = super.getView(position, convertView, parent);
localView.setBackgroundResource(selected);
return localView;
}
}
今天写程序的时候,遇到了这个问题,特留下此贴,与大家分享。