How to Bind a ListView to a To a Json String ?

DyaryDyary Member ✭✭
edited August 2018 in Xamarin.Forms

I am trying to bind my ListView listView1 to the id property of the the Json . The biding doesn't seem to work.

here Is my MainPage.cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using FetchActor.Models;
using Newtonsoft.Json;
using System.Collections.ObjectModel;

namespace FetchActor
{
    public partial class MainPage : ContentPage
    {

        HttpClient connection = new HttpClient();
        MovieService movieService = new MovieService();
        ObservableCollection<Movie> _people;
        private object jsonconvert;

        public MainPage()
        {
            InitializeComponent();
        }

        protected async override void OnAppearing()
        {

            movieService.PeoplefFirstName = "John";
            movieService.PeopleSecondName = "Travolta";


            var _connection = await connection.GetStringAsync(movieService.CreatePeopleUrl(movieService.PeoplefFirstName, movieService.PeopleSecondName));
            var people = JsonConvert.DeserializeObject<RootObject>(_connection);

            var json  = people.results;




            searchListView1.ItemsSource = json;


            base.OnAppearing();
        }
    }
}

and this is the MainPage.Xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:FetchActor"
             x:Class="FetchActor.MainPage">

    <StackLayout>
        <Editor x:Name="testEditor" />
        <SearchBar Placeholder="Search By Actor..." />
        <ListView x:Name="searchListView1" >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ImageCell Text="{Binding id}" />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>

</ContentPage>

and here is the JsonData.cs model

using System;
using System.Collections.Generic;
using System.Text;

namespace FetchActor.Models
{
    public class KnownFor
    {
        public double vote_average { get; set; }
        public int vote_count { get; set; }
        public int id { get; set; }
        public bool video { get; set; }
        public string media_type { get; set; }
        public string title { get; set; }
        public double popularity { get; set; }
        public string poster_path { get; set; }
        public string original_language { get; set; }
        public string original_title { get; set; }
        public List<int> genre_ids { get; set; }
        public string backdrop_path { get; set; }
        public bool adult { get; set; }
        public string overview { get; set; }
        public string release_date { get; set; }


    }

    public class Result
    {
        public double popularity { get; set; }
        public int id { get; set; }
        public string profile_path { get; set; }
        public string name { get; set; }
        public List<KnownFor> known_for { get; set; }
        public bool adult { get; set; }


    }

    public class RootObject
    {
        public int page { get; set; }
        public int total_results { get; set; }
        public int total_pages { get; set; }
        public List<Result> results { get; set; }


    }
}

Could you help out please?
Thanks

Best Answer

Answers

  • LuisDavidDelaCruzLuisDavidDelaCruz Member ✭✭✭

    @Dyary Hi, I had a similar case as you, I link data of a listview in code behind
    Try this:

    var ListViewSource = new List<YourModel> { };
    Your_Model info = new Your_Model();
    var _connection = await client.GetStringAsync(movieService.CreatePeopleUrl(movieService.PeoplefFirstName, movieService.PeopleSecondName));
    
        string resp = Convert.ToString(_connection );
        var obj = JsonConvert.DeserializeObject<object>(resp);
        string data = Convert.ToString(obj);
    info = JsonConvert.DeserializeObject<List<YourModel>>(data);
    
    foreach (var item in info)
                    {
                        aprobaciones.Add(new RootObject
                        {
                            page  = item.Page,
    etc...
    etcc..
                        });
                        }
    //Ending the foreach loop 
    
    searchListView1.ItemsSource = ListViewSource ;
    

    hope be useful for you

  • DyaryDyary Member ✭✭
    Hi Luis. Thank you for the answer. I actually debugged the program. it seems that after a long pause it crashes on this line
    var people = JsonConvert.DeserializeObject(_connection);

    and i get this error

    System.Net.Http.HttpRequestException: An error occurred while sending the request

    any ideas what could that be ?

Sign In or Register to comment.