We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

How to show "No Items in the list" message in xaml page when service fetched list is empty?

Hello,
          I am using syncfusion listview to display api service fetched data.But I am stuck at a point where I have to show the notification message that "No Items in the List" in case of empty list returned from api service call?

How to fix this?

here's my code below.

Attachment: 14.8_1da3e3af.zip

7 Replies

JN Jayaleshwari N Syncfusion Team August 15, 2018 01:28 PM UTC

Hi Sourav,  
  
Thanks for contacting Syncfusion support.  
  
We have checked the reported query “Need to show empty listview message in the view” from our end. Currently, SfListView does not have support to show the empty message. We have already considered it as an feature and added it into our feature request list. You can track the progress of the reported feature from the below link.     
  
  
However, you can achieve it by using the visibility property like below code snippet.  
  
Code Exampl[C#]:  
public MainPage()  
                        {  
                                    InitializeComponent();  
                                       listView.DataSource.DisplayItems.CollectionChanged += DisplayItems_CollectionChanged;  
                        }  
  
        private void DisplayItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)  
        {  
            if (listView.DataSource.DisplayItems.Count == 0)  
            {  
                listView.IsVisible = false;  
                label.IsVisible = true;  
            }  
            if (listView.DataSource.DisplayItems.Count > 0)  
            {  
                listView.IsVisible = true;  
                label.IsVisible = false;  
            }  
        }  
  
For your reference we have attached the sample an you can download it from the below link.  
  
  
Regards,  
Jayaleshwari N


SO Sourav August 16, 2018 04:55 AM UTC

Hey Jayaleshwari N
First of all thank you for reply.
I am binding my xaml page and viewmodel. Because the list data is coming from viewmodel ,direct to the xaml page. There is just InitializeComponent() in xaml.cs page.
So where should I apply the code you provided ?

                             Thanking You.
Regards,
Sourav De



JN Jayaleshwari N Syncfusion Team August 17, 2018 12:37 PM UTC

Hi Sourav,  
  
Sorry for the inconvenience.  
  
We have checked the reported query from our end. We would like to let you know that you have to derive your viewmodel class from Behaviour<ContentPage> to use CollectionChanged Event in ViewModel. We have attached the sample for your reference and please refer the sample from below link.  
  
  
Need to set behavior for content page like below:   
  
<ContentPage.Behaviors>  
        <local:EmployeeViewModel />  
</ContentPage.Behaviors>  
  
CollectionChanged event can be triggered by overriding OnAttachedTo method like below :  
  
  
  
public class EmployeeViewModel : Behavior<ContentPage>,INotifyPropertyChanged  
{  
   protected override void OnAttachedTo(ContentPage bindable)  
   {  
      ListView = bindable.FindByName<Syncfusion.ListView.XForms.SfListView>("listView");  
      label = bindable.FindByName<Label>("label");  
      viewmodel = new EmployeeViewModel();  
      ListView.BindingContext = viewmodel;  
      ListView.DataSource.DisplayItems.CollectionChanged += DisplayItems_CollectionChanged;  
            base.OnAttachedTo(bindable);  
  
    }  
  
   private void DisplayItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)  
   {  
      if (ListView.DataSource.DisplayItems.Count == 0)  
      {  
         ListView.IsVisible = false;  
         label.IsVisible = true;  
      }  
      if (ListView.DataSource.DisplayItems.Count > 0)  
      {  
         ListView.IsVisible = true;  
         label.IsVisible = false;  
      }  
  }  
}  
  
  
Please let us know if you require any further assistance.  
  
Regards,  
Jayaleshwari N. 



JH Josef Henryson January 5, 2021 02:16 PM UTC

Hi, I found this thread after I followed this example: 

https://www.syncfusion.com/kb/9956/how-to-show-empty-message-when-listview-has-no-items-in-xamarin-forms

I get a problem with the SfListView when items source is cleared and the list visibility is set to false. When I populate the list again it will appear blank. 

When debugging the code I can see that the property ItemsSource has items and the list height is changed. But the content looks empty. 

   
        x:Name="PlanningList"
        Grid.Row="0"
        AllowSwiping="False"
        AutoFitMode="DynamicHeight"
        IsScrollingEnabled="False"
        IsScrollBarVisible="False"
        IsVisible="{Binding HasPlanning}"
        ItemSpacing="1"
        ItemTapped="OnTapped"
        ItemsSource="{Binding CurrentWeek}"
        SelectionMode="None">
       
           
               
                    LineHeight="1"
                    Style="{StaticResource WeekDayLabelStyle}"
                    Text="Item text" />
           
       
   
   
        x:Name="NoPlanningLabel"
        Grid.Row="0"
        Margin="0,10,0,0"
        IsVisible="{Binding NoPlanning}"
        HorizontalTextAlignment="Center"
        FontSize="20"
        Text="No planning available" />

Any idea what might be wrong? 

UPDATE: When I replace the SfListView with Xamarin.Forms.ListView it works like a charm!



LN Lakshmi Natarajan Syncfusion Team January 6, 2021 09:38 AM UTC

Hi Josef, 
 
Thank you for using Syncfusion products. 
 
We have checked the reported scenario with shared code snippets from our side. We suspect that the issue occurs when handling the visibility with IsScrollingEnabled as False for ListView. We suggest you to handle the visibility for the NoPlanningLabel alone to achieve your requirement.  
 
We have prepared a sample based on your scenario and attached in the following link, 
 
Please let us know if you need further assistance. 
 
Regards, 
Lakshmi Natarajan 



JH Josef Henryson January 7, 2021 07:50 AM UTC

Wow, thank you for your response, this was it! Thank you very much. I can't believe I spent 2 days trying to solve this. 


LN Lakshmi Natarajan Syncfusion Team January 7, 2021 08:04 AM UTC

Hi Josef, 
 
Thank you for the update. 
 
We are glad that our solution meets your requirement. Please let us know if you need further assistance. As always we are happy to help you out. 
 
Lakshmi Natarajan 
 
 


Loader.
Live Chat Icon For mobile
Up arrow icon