【发布时间】:2017-05-18 06:36:05
【问题描述】:
我将自定义适配器中每一行的背景颜色设置为
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;
}
行背景设置为两种颜色。是不是一直生成的随机数都是一样的?
【问题讨论】:
-
比较if条件下的位置编号和颜色数组。 with 在循环中设置背景颜色以转换视图。
-
我不能这样做,因为列表是动态的,可以包含 n 个项目。
标签: android listview custom-adapter