hi,
I want to bind listview using model class filled data from httpclient request (json).. can anyone help me with sample code or tutorials.
Code behind
===========
PostContentModel postContent = new PostContentModel()
{
reqType = "DDDD",
token = "TTT",
device = "SDSDSD"
};
var json = JsonConvert.SerializeObject(postContent);
var content = new StringContent(json, Encoding.UTF8, "application/json");
HttpClient client = new HttpClient();
var response = await client.PostAsync("Requestsurl", content);
newsModel objnewsModelList = new newsModel();
if (response.IsSuccessStatusCode) // Check the response StatusCode
{
string responseBody = await response.Content.ReadAsStringAsync();
objnewsModelList = JsonConvert.DeserializeObject<newsModel>(responseBody);
listView.ItemSize = 100;
listView.ItemsSource = objnewsModelList;
listView.ItemTemplate = new DataTemplate(() => {
var grid = new Grid();
var newstitle = new Label { FontAttributes = FontAttributes.Bold, BackgroundColor = Color.Teal, FontSize = 21 };
newstitle.SetBinding(Label.TextProperty, new Binding("news_title"));
var newsdate = new Label { BackgroundColor = Color.Teal, FontSize = 15 };
newsdate.SetBinding(Label.TextProperty, new Binding("news_date"));
grid.Children.Add(newstitle);
grid.Children.Add(newsdate, 1, 0);
return grid;
});
}
Xaml View
=============
<?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:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:sflv="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms"
x:Class="TestApp.View.Home">
<sflv:SfListView x:Name="listView"
ItemsSource="{Binding newsModel}"
ItemSize="100">
<sflv:SfListView.ItemTemplate>
<DataTemplate>
<Grid Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="0.4*" />
<RowDefinition Height="0.6*" />
</Grid.RowDefinitions>
<Label Text="{Binding news_title}" FontAttributes="Bold" TextColor="Teal" FontSize="21" />
<Label Grid.Row="1" Text="{Binding news_date}" TextColor="Teal" FontSize="15"/>
</Grid>
</DataTemplate>
</sflv:SfListView.ItemTemplate>
</sflv:SfListView>
</ContentPage>