Populate listview from json

I am stuck on how to get my json data into a listview. There is no problems getting the json from the webservice and into a variable. Quite possibly there is an much easier way of doing this?

Here is the class that defines how the JSON data is contructed.

`public class Result
{
    public string Comments { get; set; }
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string FullName { get; set; }
    public string CreatedOn { get; set; }
    public string CreatedBy { get; set; }
    public string CreatedSystem { get; set; }
}

public class RootObject
{
    public int Count { get; set; }
    public List<Result> Result { get; set; }
}`



`public async Task LoadDetailNotesAsync() {
            ////Pass Information to Webservice
            Uri jsonUrl1 = new Uri(string.Format("myurl));

            var result = await myHelper.GetResponseString(jsonUrl1);

            JObject rss = JObject.Parse(result);
            int myCount = (int)rss["Count"];

            try
            {

                for (int x = 0; x <= myCount - 1; x++)
                {
                    NotesClass tcu = new NotesClass()
                    {
                        Comments = (string)rss["Result"][x]["Comments"]
                    };

                }

            }
}`

My Listview XAML file
`<ListView HeightRequest="150" x:Name="listDetail">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <Label x:Name="lblFullName" Text="{Binding FullName}"></Label>
                                <Label x:Name="lblComment" Text="{Binding Comment}"></Label>
                                <Label x:Name="lblCreatedOn" Text="{Binding CreatedOn}"></Label>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>`
Tagged:

Best Answer

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    where do you set the ItemSource?

  • RileyASIRileyASI Member ✭✭
    edited February 2018

    AlessandroCaliaro, sorry for my ignorance but that is what I am stuck on. What do I set the listDetail.ItemsSource to? Must I create an new variable to hold tcu?

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    listDetail.ItemSource = yourList (that I don't see)

  • RileyASIRileyASI Member ✭✭

    Here is what I have some up with a potential solution but it has caused a different error.
    UIKit Consistency error: you are calling a UIKit method that can only be invoked from the UI thread.

        ObservableCollection < NotesClass > myNewList = new ObservableCollection < NotesClass >();
                    try
                    {
                        for (int x = 0; x <= myCount - 1; x++)
                        {
                            myNewList.Add(new NotesClass {Comments = (string)rss["Result"][x]["Comments"], FullName= (string)rss["Result"][x]["FullName"], CreatedOn = (string)rss["Result"][x]["CreatedOn"]});
    
                        }
                        listDetail.ItemsSource = myNewList;
    

    Here is the call from my protected async override void OnAppearing() to LoadDetailNotesAsync

                await Task.Run(async () =>
                {
                    await LoadDetailNotesAsync();
                });
    
  • RileyASIRileyASI Member ✭✭

    Any suggestions in how to make this work? myNewList seems to populate just fine. Just trying to get the ItemsSource to be loaded.

    Thanks

  • seanydaseanyda GBMember ✭✭✭✭✭

    @RileyASI said:
    Any suggestions in how to make this work? myNewList seems to populate just fine. Just trying to get the ItemsSource to be loaded.

    Thanks

    You need to set an ItemSource which would be the list. Then you need to set an ItemTemplate which should be a DataTemplate built to construct a View to display the data from your list. You can use Binding to populate values e.g a text box with the data from your list.

  • RileyASIRileyASI Member ✭✭
    edited February 2018

    @seanyda said:

    @RileyASI said:
    Any suggestions in how to make this work? myNewList seems to populate just fine. Just trying to get the ItemsSource to be loaded.

    Thanks

    You need to set an ItemSource which would be the list. Then you need to set an ItemTemplate which should be a DataTemplate built to construct a View to display the data from your list. You can use Binding to populate values e.g a text box with the data from your list.

    I believe I have everything that you have mentioned. However when I do this it produces and error: UIKit Consistency error: you are calling a UIKit method that can only be invoked from the UI thread.

    <ListView HeightRequest="150" x:Name="listDetail">
    <ListView.ItemTemplate>
                            <DataTemplate>
    
                                <ViewCell>
                                    <Label Text="{Binding FullName}"></Label>
                                    <Label Text="{Binding Comments}"></Label>
                                    <Label Text="{Binding CreatedOn}"></Label>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>`
    

    listDetail.ItemsSource = myNewList;

    Should I have a ItemsSource="{Binding myNewList}" on the ListView within the XAML code?

Sign In or Register to comment.