I just upgraded my project from 2021.1.21f1 to 2021.2.0f1. Modifying the items source and calling refresh results in an inconsistent number of bindItem() callbacks. Sometimes it does, but often doesn’t match the itemSource count. Before the upgrade, modifying the item source and calling Refresh() (now deprecated) worked as expected.
Relevant code snippets:
void Awake()
{
this.stationsListElement = this.container.Q<ListView>("Stations");
this.stationsListElement.makeItem = () =>
{
// Note the items in the list view are created from uxml templates
VisualElement ve = this.stationView.CloneTree();
// Setup the new ve ...
return ve;
}
this.stationsListElement.bindItem = (ve, i) =>
{
// Setup the ve ...
};
// Setup the item source
this.filteredStations = new List<ShipStationComponent>();
this.stationsListElement.itemsSource = this.filteredStations;
}
And then at runtime, I’ll modify the item source.
private void RefreshStationsList()
{
this.filteredStations.Clear();
foreach (ShipStationComponent station in ...)
{
// ...
this.filteredStations.Add(station);
}
this.stationsListElement.RefreshItems();
}
Note: I already created a bug report for this, and am posting on the forum in case others run into this. Also, maybe I’m doing something dumb someone can point out sooner.
1 Like
Just updated to 2021.2.4f1 and this is still broken
Oh no… I just found a bug in my code that was causing this problem. Please disregard.
Hi,
Could you provide any details regarding your bug please? I have the same issue and am wondering if I’ve introduced a similar bug.
Thanks
Mick
Glad to know the problem cause was found and fixed. I am also interested in knowing what was the problem on your side :). And if possible, would you be kind enough to close the bug report (if not already done) please? Thanks a bunch!
Could you provide any details regarding your bug please?
I am also interested in knowing what was the problem on your side
Fine… time to feel dumb 
this.stationsListElement.itemsSource = this.filteredStations;
this.stationsListElement.unbindItem = (ve, i) =>
{
// The bug:
this.filteredStations.Remove((ShipStationComponent)ve.userData);
// Unbind from element callbacks (not important for bug)
ve.Q<Button>("View").UnregisterCallback<MouseUpEvent>(OnLookatStation);
CustomSlider slider = ve.Q<CustomSlider>("CustomSlider");
slider.OnValueChangedEvents -= OnStationAssignmentChanged;
}
filteredStations is the data source for the list view.
Whenever a listview item was unbound, I was removing the item associated with that visual element from the data source. It would take some other in-game action to repopulate filteredStations and so the listview appeared to be randomly missing entries.
I guess the question now is why this worked in the previous Unity version at all
would you be kind enough to close the bug report (if not already done) please?
I’m actually not entirely sure how find my bug report. I think I reported it through the editor help menu and I’m not seeing any emails
Thanks for posting details. It’s not the problem in my case but always helpful to have a list of things to check in the event of a problem.
I built a test app with a listview and couldn’t reproduce the issue. However in my actual game the listview items were set to have a Dynamic Height (in the listview properties). When I changed this to fixed height the problem went away. I tested this in the test app and it still didn’t cause the problem (but is consistently repeatable in my game). So my real UI probably has some funky set-up that causes the problem.
I guess the question now is why this worked in the previous Unity version at all
We changed ListView some time ago and we now reuse elements as much as possible, not keeping stuff lying around too much anymore. That may have been why it just worked in the past but not with the more recent versions 