Skip to content Skip to sidebar Skip to footer

How It Is Possible 5 Different Colors Of Each Row In Listview If Items Are More Then Ten In Listview In Android?

Make an array as given below as no of list item i suppose u have five items: int[] color_arr={Color.BLUE,Color.CYAN,Color.YELLOW,Color.GREEN,Color.RED}; and using it in getView()

Solution 1:

you are having 5 values in array, so if list is having more than 5 items definitely it will crash because it can not find data_image[5]

You can use % operator

imageview.setBackgroundColor(data_image[position % 5]);

For 0-4th position it will work as its working now, for 5th position it will set color of position 0, for 6th position it will set color of position 1 and so on...

Solution 2:

row.setBackgroundColor(color_arr[position % color_arr.length]);

Solution 3:

Try this,

row.setBackgroundColor(color_arr[position % color_arr.length]);

Post a Comment for "How It Is Possible 5 Different Colors Of Each Row In Listview If Items Are More Then Ten In Listview In Android?"