我设置每一行的背景颜色在我customadapter数组列表视图中的每一行作为随机颜色从颜色
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.coupon_list_row, null);
int temp_index = ExtraFu.randInt(0, 9);
convertView.setBackgroundColor(color_arr[temp_index]);
Log.d("temp_index", String.valueOf(temp_index));
}
我的色彩阵列
int color_arr[]={R.color.cred,R.color.cpink,R.color.cpurple,R.color.cdpurple,R.color.cindigo,R.color.cblue,R.color.cdorange,R.color.cgreen,R.color.cbroun,R.color.ccyan};
这是函数randInt
public static int randInt(int min, int max) {
// Usually this can be a field rather than a method variable
Random rand = new Random();
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
行背景设置为两种颜色。那个随机数是否始终生成一样?
比较位置n条件下的数字和颜色数组。在循环中设置convertview的背景颜色。 –
我不能这样做,因为列表是动态的,可以包含n个项目。 –