Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Why my ObservableCollection is zero

$
0
0

Hi,

I am using below code to populate weather data from OpenWeatherMap and I am getting the data read by ReadAsStringAsync but my ObservableCollection is zero count when I checked.

Why is that if anyone can help please?

here is my code:

private async void Get5DaysForecast()
{
    var location_data = await GetLocation();

    string url = "https://api.openweathermap.org/data/2.5/forecast?lat=" + location_data.latitude + "&lon=" + location_data.longitude + "&appid=" + api_key;

    var client = new HttpClient();

    client.BaseAddress = new Uri(url);

    var response = await client.GetAsync(url);

    await DisplayAlert("response", Convert.ToString(response), "OK");

    var data = await response.Content.ReadAsStringAsync();

    await DisplayAlert("data", Convert.ToString(data), "OK");

    var result = JsonConvert.DeserializeObject<WeatherInfo.List>(data);

    await DisplayAlert("result", Convert.ToString(result), "OK");

    ObservableCollection<WeatherInfo.List> trends = new ObservableCollection<WeatherInfo.List>();

    await DisplayAlert("trends count", Convert.ToString(trends.Count), "OK");

    ListViewWeatherForecast.ItemsSource = trends;

    loadingWeatherForecast.IsVisible = false;

    ListViewWeatherForecast.IsVisible = true;
}

and this is my Model:

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

namespace Samana
{
    class WeatherInfo
    {
        public class Coord
        {
            public double lon { get; set; }
            public double lat { get; set; }
        }

        public class Weather
        {
            public int id { get; set; }
            public string main { get; set; }
            public string description { get; set; }
            public string icon { get; set; }
        }

        public class List
        {
            public int dt { get; set; }
            public Main main { get; set; }
            public List<Weather> weather { get; set; }
            public Clouds clouds { get; set; }
            public Wind wind { get; set; }
            public Sys sys { get; set; }
            public string dt_txt { get; set; }
            public Rain rain { get; set; }
            public Snow snow { get; set; }
        }

        public class City
        {
            public int id { get; set; }
            public string name { get; set; }
            public Coord coord { get; set; }
            public string country { get; set; }
        }

        public class Main
        {
            public double temp { get; set; }
            public double temp_min { get; set; }
            public double temp_max { get; set; }
            public double pressure { get; set; }
            public double sea_level { get; set; }
            public double grnd_level { get; set; }
            public int humidity { get; set; }
            public double temp_kf { get; set; }
        }

        public class Wind
        {
            public double speed { get; set; }
            public double deg { get; set; }
        }

        public class Clouds
        {
            public int all { get; set; }
        }

        public class Sys
        {
            public double message { get; set; }
            public string country { get; set; }
            public int sunrise { get; set; }
            public int sunset { get; set; }
            public string pod { get; set; }
        }

        public class Rain
        {
            public double __invalid_name__3h { get; set; }
        }

        public class Snow
        {
            public double __invalid_name__3h { get; set; }
        }

        public class Root
        {
            public double message { get; set; }
            public int cnt { get; set; }
            public List<List> list { get; set; }
            public City city { get; set; }
            public Coord coord { get; set; }
            public List<Weather> weather { get; set; }
            public string @base { get; set; }
            public Main main { get; set; }
            public Wind wind { get; set; }
            public Clouds clouds { get; set; }
            public int dt { get; set; }
            public Sys sys { get; set; }
            public int id { get; set; }
            public string name { get; set; }
            public string cod { get; set; }
        }
    }
}

Thanks,
Jassim


Viewing all articles
Browse latest Browse all 77050

Trending Articles