0

I am trying to update color of listview from async task postexecute method.

I am doing ((View) lstChoices.getAdapter().getView(0,null, lstChoices)).setBackgroundColor(Color.RED);

But this is not doing anything, but I tried the same in getView method of my custom adapter then it worked, row.setBackgroundColor(Color.RED); Any ideas what I am doing wrong?

Thanks

Mr_Hmp
  • 2,474
  • 2
  • 35
  • 53
  • you should never manually change the views background, you should be using a selector as the background and use `listview.setItemChecked()` – tyczj Jun 24 '15 at 18:58
  • I want to update the listview's rows background color inorder to represent the result – Mr_Hmp Jun 24 '15 at 19:01
  • And you can do that with a selector and using setItemChecked – tyczj Jun 24 '15 at 19:02

2 Answers2

1

You should not change this that way. All what is related to the row should be handled in adapter. It means that if you changed anything that could influence background color, then all you should do is notifyDatsetChanged() which would trigger list redraw. And b/g color should be then changed by adapter.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • So, after notifyDatsetChanged(), the getView method of adapter will be called? – Mr_Hmp Jun 24 '15 at 19:02
  • 1
    `notifyDatasetChanged()` tells adapter, well... that data it lists has changed and this would trigger list redraw, because list wants to reflect current data, after the change. And redraw would call `getView()` for each visible row. – Marcin Orlowski Jun 24 '15 at 19:06
0

A very clearly presented approach to Listview Item Background color changing based on the item's State is at Programmatically select item ListView in Android

Since you are looking for 'postexecute' perhaps changing the Item's State and using that approach can help you get what you are after.

Community
  • 1
  • 1
Dhugalmac
  • 554
  • 10
  • 20