I have a ListView in my ListActivity which is inflated with an adapter. There is a Handler and i call its postDelayed() method to run a refresh() recursively of the ListView (with the help of notifyDataSetChanged()). postDealyed() is set to 1 second.
Now, there are ContextMenus for all the item/rows of this ListView.
I want to set a row’s/item’s background separately from its ContextMenu using this code (with getting the position of the “longpressed” row/item):
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
...
case R.id.highlight:
ListView lv = (ListView)getListView();
View row = (View) lv.getChildAt(info.position);
row.setBackgroundColor(Color.argb(255, 22, 100, 100));
return true;
...
When i run the app the selected row’s background gets changed (as it should), however every second (so at every refresh of the ListView) another row of it changes and the selected row changes back to its original state. After another second everything changes back. If i slide over the rows of the ListView other rows are getting flashing aswell. Just like if the references were changing or i really don’t know. If i stop sliding the flashing backgrounds have a pattern until the next slide when it gets another pattern.
Any ideas?
Thanks in advance!
That’s because your Views are getting recycled. Your “highlighting” code needs to update the backing model of the list item and set its “highlighted” property to true, it should not be changing the View directly. When you render your item (in your adapter) you check if this property is true, if it is, then use the background you specified, otherwise set it back to the default.