SListView does not appear after adding items to an initially empty array

I found that SListView does not show up, if you bind the item source to an initially empty array then add some items.

class ListViewPage : public SCompoundWidget
{
	TSharedPtr<SButton> m_addButton;
	TArray<TSharedPtr<FString>> m_items;
	int m_numItems{  };
public:
	ListViewPage()
	{
		//This has to be pre-populated, otherwise nothing shows up
		//for (int i = 0; i < 20; ++i)
		//{
		//	m_items.Add(MakeShared<FString>(FString{ TEXT("Item #") } + FString::FromInt(m_numItems++)));
		//}
	}

	auto onAddButtonClick()
	{
		m_items.Add(MakeShared<FString>(FString{ TEXT("Item #") } + FString::FromInt(m_numItems++)));
		return FReply::Handled();
	}

	SLATE_BEGIN_ARGS(ListViewPage) {}
	SLATE_END_ARGS()

	void Construct(FArguments const& InArgs)
	{
		ChildSlot
		[
			SNew(SVerticalBox)
			+ SVerticalBox::Slot()
			[
				SAssignNew(m_addButton, SButton)
				.Text(LOCTEXT("AddItem", "Add item"))
				.OnClicked(this, &ListViewPage::onAddButtonClick)
			]
			+ SVerticalBox::Slot()
			[
				
				SNew(SListView<TSharedPtr<FString>>)
				.ItemHeight(24)
				.ListItemsSource(&m_items)
				.OnGenerateRow_Lambda(
				[this](TSharedPtr<FString> inItem, TSharedRef<STableViewBase> const& ownerTable)
				{
					return SNew(STableRow<TSharedRef<STextBlock>>, ownerTable)
						[SNew(STextBlock).Text(FText::FromString(*inItem))];
				}
				)
			]
		];
	}
};

After clicking the button, the onAddButtonClick() handler is called, but the OnGenerateRow_Lambda is not called (break point does not hit). Uncomment the constructor would solve the issue, but that’s not what I want.

Try calling RequestListRefresh on the list view after modifying the source.

1 Like

Thanks, that works. The sad thing is it’s nowhere mentioned in the doc.