我設置每一行的背景顏色在我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個項目。 –