print xaml list in xamarin (foreach)

100 Views Asked by At

I have a problem, or rather, I don't know how to do it.

Consider the JSON type data coming from the API extension as follows.

DATA:[
{
OrderNo:66753,
Orders:[
{
Code:F-EN-01,
Name:Test1,
Quantity: 100,
},
{
Code:F-EN-05,
Name:Test2,
Quantity: 505,
},
...
]},
{
OrderNo:66754,
Orders:[
{
Code:F-EN-03,
Name:TestOr1,
Quantity:2000,
},
{
Code:F-EN-09,
Name:TestOr2,
Quantity: 4500
},
...
]},
...

Imagine going this way. What I want to do is to show it on the screen by grouping it according to the OrderNo data. I have attached the picture for complete understanding. I can do this using foreach inside foreach but i don't know how i can do it in xamarin xml side. enter image description here Thanks for your help, keep up the good work.

We can use a structure like this in normal ASP .NET MVC:

public class OrderDetail

{
   public string ProductCode {get;set;}
   public string ProductPrice {get;set;}
   public string ProductCount {get;set;}
   
}

public class Order
{
   public string orderNo {get;set;}
   public List<OrderDetail> orderDetails {get;set;}
}

List<Order> orderList=new List<Order>();

foreach (var item in orderList)
{
    <h1> item.orderNo() <h1>

foreach (var orderDetail in item.orderDetails)
{
<td> orderDetail.ProductCode() <td>
<td> orderDetail.ProductPrice() <td>
<td> orderDetail.ProductCount() <td>
}

}

I can print a list of objects in ListView with xamarin, yes, but if there is a field with an array in the objects I print, how do I print it?

1

There are 1 best solutions below

0
Kemal On

This is how I solved the problem.

<ListView x:Name="orderListView" HasUnevenRows="True" SelectionMode="None" ItemTapped="orderListView_ItemTapped">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Vertical">
                            <Frame CornerRadius="15"
                                   Margin="5,5,5,5"
                                   HasShadow="True"
                                   BackgroundColor="#FDFEFE">
                                <StackLayout Orientation="Vertical">
                                    <Label Text="{Binding OrderNumber, StringFormat='Sipariş Numarası : {0:d}'}"></Label>

                                    <StackLayout Margin="0,15,0,0" BindableLayout.ItemsSource="{Binding OrderDetails}">
                                        <BindableLayout.ItemTemplate>
                                            <DataTemplate>
                                                <StackLayout>
                                                    <Grid>
                                                        <Grid.RowDefinitions>
                                                            <RowDefinition Height="Auto"></RowDefinition>
                                                        </Grid.RowDefinitions>
                                                        <Grid.ColumnDefinitions>
                                                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                                                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                                                            <ColumnDefinition Width="*"></ColumnDefinition>
                                                        </Grid.ColumnDefinitions>

                                                        <Label FontSize="12" Grid.Column="0" Grid.Row="0" Text="{Binding ProductCode}"></Label>
                                                        <Label FontSize="12" Grid.Column="1" Grid.Row="0" Text="{Binding ProductName}"></Label>
                                                        <Label HorizontalOptions="End" FontSize="12" Grid.Column="2" Grid.Row="0" Text="{Binding Amount}"></Label>

                                                    </Grid>
                                                    <BoxView Color="Black" HeightRequest="1" HorizontalOptions="FillAndExpand"></BoxView>
                                                </StackLayout>

                                            </DataTemplate>
                                        </BindableLayout.ItemTemplate>
                                    </StackLayout>
                                </StackLayout>
                            </Frame>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

show picture

My problem was that I could not print the list in the object to the screen.

"List orderDetails;" in "Order" object I had a fiel of type "orderDetails" that was my aim to print on the screen. I wanted a screen as seen in the picture.