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

how to get json data from api in listview paging

i want to get data from web api in listview with paging. What i have done so far i dont see any problem in my code but i am getting null values in  Handle_OnDemandLoading
here is my code
 private FeaturedItemList products = new FeaturedItemList();  
        protected async void FeaturedList()
        {

   var httpClient = new HttpClient();
                      

                        var json = await httpClient.GetStringAsync("http://orangepotato.rjcpacking.com/index.php?route=api/login/getFeaturedProducts");


                        products = JsonConvert.DeserializeObject<FeaturedItemList>(json);
               
                        dataPager.Source = products.products;



        }
  
void  Handle_OnDemandLoading(object sender, Syncfusion.SfDataGrid.XForms.DataPager.OnDemandLoadingEventArgs e)
        {
            var source= products.products.Skip(e.StartIndex).Take(e.PageSize);
            FeaturedlistView.ItemsSource = source.AsEnumerable();// here is i am getting null values but i am getting values in datapager.source
        }
  

kindly need to resolove this issue asap

5 Replies

DY Deivaselvan Y Syncfusion Team April 1, 2019 06:29 AM UTC

Hi Waqas, 

Thank you for contacting Syncfusion support.

 
We have checked the reported query from our side. You can deserialize and load the data into ItemsSource collection like below. We have attached the tested sample using SfDataPager and SfListView which fetches the data from respected API service using OnDemandLoading, please find the sample from below and code snippet for your reference.  
  
  
  
In ViewModel  
public async System.Threading.Tasks.Task<ObservableCollection<Data>> RefreshDataAsync()  
{  
      Collection = new ObservableCollection<Data>();  
      RestUrl = "Api service link";  
      var uri = new Uri(RestUrl);  
      try  
      {  
          var response = await client.GetAsync(uri);  
  
          if (response.IsSuccessStatusCode)  
          {  
            var content = await response.Content.ReadAsStringAsync();  
            Items = JsonConvert.DeserializeObject<FullData>(content);  
  
            for (int i = 0; i < 5; i++)  
            {  
               var data = new Data()  
               {  
                 _Name = Items [i].Name,  
                 _Id=Items[i].Id  
               };  
               Collection.Add(data);  
             }  
             return Collection;  
           }  
        }  
 
 
In Behavior.cs 
 
dataPager.OnDemandLoading += DataPager_OnDemandLoading; 
 
private void DataPager_OnDemandLoading(object sender, OnDemandLoadingEventArgs e) 
{ 
    Device.BeginInvokeOnMainThread(async () => { 
      await Task.Delay(100); 
      var source = viewModel.ItemsSource.Skip(e.StartIndex).Take(e.PageSize); 
      ListView.ItemsSource = source.ToList<Data>(); 
      dataPager.LoadDynamicItems(e.StartIndex, source); 
      dataPager.PagedSource.UseOnDemandPaging = true; 
    }); 
} 
  
Please let us know if you need any further assistance.  
  
Regards,  
Deivaselvan 



WR waqas rauf April 1, 2019 06:54 PM UTC

sir the link u have send i have tried exactly same with the code but its not working. Here is my code this code absolutely work fine with android, but in ios data doesnt display but can pagination to next and previous.
 public partial class Home : ContentPage
    {
  
 public Home ()
        {
            InitializeComponent ();

            FeaturedList();
          
            dataPager.OnDemandLoading += DataPager_OnDemandLoading;
  

} 

        protected async void FeaturedList()
        {


            try
            {
                var connection = CrossConnectivity.Current.IsConnected;
                if (connection == true)
                {
                    try
                    {
                        //activity.IsRunning = true;
                        //activity.IsVisible = true;


                        var httpClient = new HttpClient();
                        

                        var json = await httpClient.GetStringAsync("http://orangepotato.rjcpacking.com/index.php?route=api/login/getFeaturedProducts");


                        products = JsonConvert.DeserializeObject<FeaturedItemList>(json);
                       
                       

                        foreach (var item in products.products)
                        {
                            products list = new products();
                            list.discount_percentage = item.discount_percentage;
                            list.name = item.name;
                            list.price = item.price;
                            list.special = item.special;
                            list.thumb = item.thumb;
                            if (item.special.ToString() == "0")
                            {
                                list.discount = "White";
                                list.price = item.price;
                            }
                            else
                            {
                                list.discount = "#f29d30";
                                list.price = item.special;
                            }
                            FeaturedFinalList.Add(list);

                        }

                        dataPager.Source = FeaturedFinalList;



                       

                    }
                    catch (Exception ex)
                    {

                        var exx = ex.Message;
                        await DisplayAlert("Alert", ex.Message, "Okay", "Cancel");
                    }
                    finally
                    {
                        

                    }
                }
                else
                {
                    await DisplayAlert("Message", "Please check your internet connection", "ok");

                }

            }
            catch (WebException ex)
            {

                var exx = ex.Message;
                await DisplayAlert("Message", ex.Message, "ok");
            }

        } 

 private void DataPager_OnDemandLoading(object sender, OnDemandLoadingEventArgs e)
        {
            Device.BeginInvokeOnMainThread(async () => {
                await Task.Delay(100);
                var source = FeaturedFinalList.Skip(e.StartIndex).Take(e.PageSize);
                FeaturedlistView.ItemsSource = source.ToList<products>();
                dataPager.LoadDynamicItems(e.StartIndex, source);
                dataPager.PagedSource.UseOnDemandPaging = true;
            });
        }
  
}


DB Dinesh Babu Yadav Syncfusion Team April 2, 2019 10:28 AM UTC

Hi Waqas, 

 
We recommend you to initialize the SfListView for each platform to render it. Please refer the following documentation for more reference. 
 
 
Please let us know if you need any further assistance.   
   
Regards, 
Dinesh Babu Yadav 
 



WR waqas rauf April 2, 2019 10:37 AM UTC

i have renderer listview in ios  and i found something strange, sometime it work fine on ios when i debug app again it disappear, but it doesnt happen with android. I really dont understand what is the issue.



DB Dinesh Babu Yadav Syncfusion Team April 3, 2019 08:58 AM UTC

Hi Waqas, 
 
We suspect that the issue may occur at the application when the data is fetched from the server link. So, can you please check whether the collection is updated properly to Source parameter in the  “dataPager.LoadDynamicItems(e.StartIndex, source)” method while debugging the application and when page is navigated through data pager?.  
 
If you are still facing the issue, please share the video link and if possible replicate the issue by modifying the given sample in the previous update which would help us to analyze the issue better and update you an appropriate solution. 
 
Regards, 
Dinesh Babu Yadav 


Loader.
Live Chat Icon For mobile
Up arrow icon