1

I need to dynamically change the background color of a child view of a ListView at a specific index. So I need to get the view first before applying the background.

I have tried the following.

getViewAt(int index) - this is not applicable to ListView.

getChildAt(int index) - this gives runtime NPE error

A Google search returns irrelevant results.

Please help me out. Thank you.

David Heisnam
  • 2,463
  • 1
  • 21
  • 32

2 Answers2

3

I figured out how to do this.

I really think that one should not have to post code and logcat for a question that is similar to "How to set background image".

Anyway my answer is, in short, set position as tag for every child in the listview through your adapter's getView() and then get any child using findViewWithTag(Object tag).

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    convertView = (LinearLayout)inflater.inflate(R.layout.some_layout, parent, false);        .
    convertView.setTag(String.valueOf(position));
}

And whenever you need to get a specific child.

View v = mylistview.findViewWithTag(String.valueOf(index));

To change background color.

v.setBackgroundResource(R.color.some_color);
David Heisnam
  • 2,463
  • 1
  • 21
  • 32
  • This is the answer that I was expecting. And it is exactly what the title asks. So whoever marked this as duplicate, could you tell me if this answer is in the question that this one is a duplicate of? – David Heisnam Oct 13 '14 at 09:35
1

Use getChildAt(index) from ViewGroup to get View.

Torbik
  • 489
  • 3
  • 10