开发者

How to changue the background colour of a item of a listview programatically?

i have a listview, and i need to changue the background colour of a item, for example the item 2.

I tryed with this code but it crash because the third line gives nullpoint开发者_开发问答erexception, but the list haves 11 items!!! i dont understand where is the problem

CalendarList = (ListView) findViewById(R.id.CalendarList);
CalendarList.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, MyApplication.roundMatches(CalendarPicker.getValue())));
CalendarList.getChildAt(2).setBackgroundColor(0xFFFF0000);


The problem is that the ListView is highly optimized to only create the views needed to be actually shown on the screen. So, getChildAt() will return null for rows that have not been created. Just check for a null return from getChildAt(). If row 2 (in this case) does not yet exist, it will later get created when the row is inflated (with getView(), bindView(), etc). You can just set the appropriate color at that time.


You should use setBackgroundResource(R.color.myRow_red) instead of setBackgroundColor(). In your example background color is assigned with the ID instead of the actual color described in the resources. but first you should check getChildAt() return null for rows that have been created or not? before you are applying the setBackgroundColor()


finally none of these solutions works, i did this by a different way, changing the colour of the text and not of the background

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜