Search code examples
xamarinxamarin.formsxamarin.androidxamarin-studioxamarin-binding

Xamarin rest api


Binding does not work in my code. what's wrong in this code?

HttpClient client = new HttpClient();
var response = await client.GetAsync(string.Format("uri link"));
string jsonstring = await response.Content.ReadAsStringAsync();
RootObject item = JsonConvert.DeserializeObject<RootObject>(jsonstring);
titles.ItemsSource =item.ToString();

XAML code

<ListView x:Name="titles" HasUnevenRows="False" >
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.View>
                    <StackLayout Orientation="Horizontal">
                           <Label Text="{Binding note}"/>
                       </StackLayout>
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

class object:

public class Result
{
    public string note { get; set; }
}
public class Response
{
    public List<Result> results { get; set; }
}
public class RootObject
{
    public Response response { get; set; }
}

Solution

  • you bind the lable to the note, but you set the titles.ItemsSource to the RootObject. the RootObject class doesn't have note. note is in Result class.

    and you can't set the itemsource like that.

    I suggest you to do this

    var listItem = JsonConvert.DeserializeObject<List<Result>>(jsonstring);
    titles.ItemsSource = l;