I am trying to bind a response from a JsonConvert into a ListView, I´m pretty sure this can´t be done like this, do I really have to use Observable Collection?
using Newtonsoft.Json; using System.Collections.Generic; using System.Net.Http; using Xamarin.Forms; using Xamarin.Forms.Xaml; using ModelsLayer; using System.Collections.ObjectModel; namespace TestXa1 { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class Profiles : ContentPage { public class RootObject2 { public int Status { get; set; } public List<MdlProfiles> Response { get; set; } } public class WebInterface { public HttpClient _client; public WebInterface(HttpClient httpClient) { _client = httpClient; } ListView MyListView = new ListView(); private const string url1 = "http://xxxx.xxx.xxx.xxxx/api/profiles"; protected async void OnGetList() { var content = await _client.GetStringAsync(url1); var tr = JsonConvert.DeserializeObject<RootObject2>(content); MyListView.ItemsSource = tr.Response; } } } }
And ListView
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="TestXa1.Profiles"> <ListView x:Name="MyListView"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <StackLayout> <Label Text="{Binding Id}" /> <Label Text="{Binding FirstName}" /> <Label Text="{Binding LastName}" /> <Label Text="{Binding Zip}" /> <Label Text="{Binding City}" /> </StackLayout> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView> </ContentPage>
And I´m getting nothing as a result, blanks, however, if I go to http://xxxx.xxx.xxx.xxxx/api/profiles, the json array is there, I just can´t seem to find the mistake.
There is just so much wrong with this.
You are just creating a ListView out of thin air, not even in the scope of a page. Giving it the same name does not make it correspond to the ListView you defined in your XAML.
Then you are setting, not binding, the ItemsSource. There is a difference.
Answers
It seems correct.
Try with
If you want the ListView to automatically update as items are added, removed and changed in the underlying list, you'll need to use an ObservableCollection. ObservableCollection is defined in System.Collections.ObjectModel and is just like List, except that it can notify ListView of any changes.
Tutorial about ListView Data Sources: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/listview/data-and-databinding#itemssource
Ooh I got it, but no, this list won´t ever change.
Is it a problem to access "http://xxxx.xxx.xxx.xxxx/api/profiles"; if it´s located inside the IIS? This could be the problem as well. I mean, will the emulator "find" the IIS?
Put a breakpoint on the code that calls JSON deserialize and check what was returned from your web call.
There is just so much wrong with this.
You are just creating a ListView out of thin air, not even in the scope of a page. Giving it the same name does not make it correspond to the ListView you defined in your XAML.
Then you are setting, not binding, the ItemsSource. There is a difference.
Thanks for the reply Joe
About the ListView constructor, I thouht that was completely right, do I have to use a observableCollection to initiate it even if it´s a static list?
A good bind would be like this, right? ( ItemsSource="{Binding Positions}" )
I thought that ItemsSource="{Binding Positions}" wouldn´t return my items as columns..
When you created this page, the .xaml.cs file should have come with a default constructor, which called the method
InitializeComponent()
. Where is that, and the rest of the code of your actual page class? Are you actually creating an instance ofWebInterface
somewhere and callingOnGetList()
, or do you expect code to execute just because you wrote it? I don't know where to actually start helping you, because it seems like that might involve teaching you the basics of object-oriented programming, much less Xamarin.Forms.If you omitted any code from this page in your original post, share it. Then we can get into explaining what you've done wrong and fixing it.
Yes, it is there:
Only omitted this...
The Master code that calls 'Profiles':
Once I (new Views.Profiles()); the InitializeComponent start @the Profiles page which is what it really should do and goes to:
Updates, now I can see the variables response with the json array and the var profs with the number of array entries, however, still unable to populate the ListView in the emulator:
XAML
OK, ListView is now displaying, fixed by:
However, it just displays two labels, the other ones are suppressed
The ListView code is correct, please check the data of list 'profs'.
The ListView didn´t print out all of the Labels, had to concatenate all of the Models "get" values inside a string and voilá, binded everything inside one Label only, everything working now, thanks everyone!:
Code Update:
XAML:
Code-Behind: