tags:

views:

105

answers:

3

Hello everyone,

I need a small help in List view. I am generating a list View, but whenever i do getChildAt(int position) it throws null pointer exception.

Here is the code

_list=(ListView)findViewById(android.R.id.list);

_loadListElements();

_showListUI();

_list.getChildAt(1).setBackgroundColor(Color.WHITE);
+1  A: 

If you don't have more than one child element then one will certainly give you a NullPointerException as you have to start counting from null upwards in prgramming. So you might want to try this.

_list.getChildAt(0).setBackgroundColor(Color.WHITE);

But without more code and a logcat extract of the error stack it is hard to tell.

Octavian Damiean
kkkkkk i will try pasting some more details from log cat, at present i am not at my workplace. What i wanted was to get a view of a single row in the list view. The same view that is returned when a list item is clicked?????
viv
+1  A: 

Without knowing quite what functionality you are trying to achieve, I'm not sure if either of these suggestions will be relevant.

Instead of using list.getChildAt(), can you set the background color from within the getView() method of whatever Adapter you are using? You'll have the view, but I don't know if you'll have the data yet to know which one to change the background on.

From one of your comments, it sounds like you are wanting to get the view of whatever list item was just clicked. The view "v" provided in ListActivity.onListItemClick() seems like it should be the view that you are wanting to work with.

Hope one of those two helps.

John
Ya, I initially did it inside getView() only, but it was not giving desired results. It was setting background of more than one rows. So i thought of jumping to the view of particular row after the list is created. For ex .. color the background of 4th row of the list view after the list has been created. I am still trying if i will get some solution, i will post it over here..... Thanks for the response
viv
A: 

Got it working............. In the getView(), the reason why it was not working earlier according to me was that,

For a condition check... for ex : if(condition matched) change the color;

This was causing problem when list used to update itself......

So correct would be : if(condition) [do something]; else [restore to original]

It was because that i did not had else statement i was getting problem.

viv