Home > Enterprise >  Xamarin ListView not show json results from api
Xamarin ListView not show json results from api

Time:02-11

same as title,my listview doesn't show me anything,i think my model in that app is not good help! deseeerialized json in jsonObject then create a list of objectModel but not show me thanks

public class Marvel
{
}
public class TextObject
{
    public string type { get; set; }
    public string language { get; set; }
    public string text { get; set; }
}

public class Url
{
    public string type { get; set; }
    public string url { get; set; }
}

public class Series
{
    public string resourceURI { get; set; }
    public string name { get; set; }
}

public class Variant
{
    public string resourceURI { get; set; }
    public string name { get; set; }
}

public class Date
{
    public string type { get; set; }
    public DateTime date { get; set; }
}

public class Price
{
    public string type { get; set; }
    public double price { get; set; }
}

public class Thumbnail
{
    public string path { get; set; }
    public string extension { get; set; }
}

public class Image
{
    public string path { get; set; }
    public string extension { get; set; }
}

public class Item
{
    public string resourceURI { get; set; }
    public string name { get; set; }
    public string role { get; set; }
    public string type { get; set; }
}

public class Creators
{
    public int available { get; set; }
    public string collectionURI { get; set; }
    public List<Item> items { get; set; }
    public int returned { get; set; }
}

public class Characters
{
    public int available { get; set; }
    public string collectionURI { get; set; }
    public List<Item> items { get; set; }
    public int returned { get; set; }
}

public class Stories
{
    public int available { get; set; }
    public string collectionURI { get; set; }
    public List<Item> items { get; set; }
    public int returned { get; set; }
}

public class Events
{
    public int available { get; set; }
    public string collectionURI { get; set; }
    public List<object> items { get; set; }
    public int returned { get; set; }
}

public class Result
{
    public int id { get; set; }
    public int digitalId { get; set; }
    public string title { get; set; }
    public int issueNumber { get; set; }
    public string variantDescription { get; set; }
    public string description { get; set; }
    public DateTime modified { get; set; }
    public string isbn { get; set; }
    public string upc { get; set; }
    public string diamondCode { get; set; }
    public string ean { get; set; }
    public string issn { get; set; }
    public string format { get; set; }
    public int pageCount { get; set; }
    public List<TextObject> textObjects { get; set; }
    public string resourceURI { get; set; }
    public List<Url> urls { get; set; }
    public Series series { get; set; }
    public List<Variant> variants { get; set; }
    public List<object> collections { get; set; }
    public List<object> collectedIssues { get; set; }
    public List<Date> dates { get; set; }
    public List<Price> prices { get; set; }
    public Thumbnail thumbnail { get; set; }
    public List<Image> images { get; set; }
    public Creators creators { get; set; }
    public Characters characters { get; set; }
    public Stories stories { get; set; }
    public Events events { get; set; }
}

public class Data
{
    public int offset { get; set; }
    public int limit { get; set; }
    public int total { get; set; }
    public int count { get; set; }
    public List<Result> results { get; set; }
}

public class Rooto
{
    public int code { get; set; }
    public string status { get; set; }
    public string copyright { get; set; }
    public string attributionText { get; set; }
    public string attributionHTML { get; set; }
    public string etag { get; set; }
    public Data data { get; set; }
}

}

mainpage

 private async void Button_Clicked(object sender, EventArgs e)
    {
        var texto = caja.Text;
        var request = new HttpRequestMessage();
        request.RequestUri = new Uri("https://gateway.marvel.com/v1/public/comics?title=hulk&ts=1&apikey=###&hash=###");
        request.Method = HttpMethod.Get;
        request.Headers.Add("Accept", "application/json");
        
        var client = new HttpClient();

        HttpResponseMessage response = await client.SendAsync(request);

        string content = await response.Content.ReadAsStringAsync();
        Result dataObjects = JsonConvert.DeserializeObject<Result>(content);

        JObject myJObject = JObject.Parse(content);
        Console.WriteLine(myJObject);

        List<Result> parsedFields = new List<Result>();
        parsedFields.Add(dataObjects);
        
        ListDemo.ItemsSource = parsedFields;

my xaml and json

https://gateway.marvel.com/v1/public/comics?title=hulk&ts=1&apikey=ab07bf416406297274b27ca941ba3bee&hash=cf9eb501d7c3775c32b72c61a6a76805

<ContentPage.Content>
    <StackLayout>

        <Label Text="Control ListView"  FontSize="40" 

       HorizontalOptions="Center" 

       VerticalOptions="CenterAndExpand" />
        <Button Text="Lamado API" Clicked="Button_Clicked"/>
        <Entry x:Name="caja" Text=""/>

        <ListView x:Name="ListDemo">

            <ListView.ItemTemplate>

                <DataTemplate>

                    <ViewCell>

                        <StackLayout Orientation="Horizontal">

                            <Image Source="{Binding resourceURI}" 

                               WidthRequest="50" 

                               HeightRequest="50"/>

                            <StackLayout Orientation="Vertical">

                                <Label Text="{Binding title}" 

                                   FontSize="15" TextColor="Blue"/>

                                <Label Text="{Binding isbn}" 

                                   FontSize="12" TextColor="Fuchsia"/>

                            </StackLayout>

                        </StackLayout>

                    </ViewCell>

                </DataTemplate>

            </ListView.ItemTemplate>

        </ListView>

    </StackLayout>
</ContentPage.Content>

tnaka

CodePudding user response:

You try binding it to an observablecollection.

private ObservableCollection<Result> _parsedFields = new ObservableCollection<Result>();

public ObservableCollection<Result> ParsedFields {

     get{ return _parsedFields;}
     set{ 
          _parsedFields = value ;
          OnPropertyChanged("ParsedFields"); 
        }
}

make sure to implement INotifyPropertyChanged

public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }

then in your button function:

var dataObjects = JsonConvert.DeserializeObject<ObservableCollection<Result>>(content);

ParsedFields = dataObjects;
ListDemo.ItemsSource = ParsedFields;

CodePudding user response:

The root level is supposed to be Root not Result class .

Modify your code as below

Root dataObjects = JsonConvert.DeserializeObject<Root>(text);         
ListDemo.ItemsSource = dataObjects.data.results;
  • Related