简体   繁体   中英

How can I display an array of arrays in a ListView (Xamarin Forms)

I want to read an array of arrays from api, and make a ListView resource for each array inside de main array in Xamarin Forms.

I actually make the class model for the api resource, I tried to make a foreach element of my rootobject, and put them into a Observable Collection and set that as a ItemSource of my listview.

this is my xaml

<StackLayout>
     <!--ListView de Clientes-->
     <ListView x:Name="ListClientes">
        <ListView.ItemTemplate>
             <DataTemplate>
                    <ViewCell>
                        <StackLayout>
                            <Label Text="{Binding cliente}" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

        <!--ListView de Proveedores-->
       <ListView x:Name="ListProveedores">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout>
                            <Label Text="{Binding proveedor}" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

        <!--ListView de Trasnportistas-->
       <ListView x:Name="ListTransportistas">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout>
                            <Label Text="{Binding transportista}" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>

this is my class model for the api resource

public class ArrImportExport
    {
        [JsonProperty("id_import_export")]
        public string id_import_export { get; set; }

        [JsonProperty("cliente")]
        public string cliente { get; set; }
    }

    public class ArrProveedore
    {
        [JsonProperty("id_proveedor")]
        public string id_proveedor { get; set; }

        [JsonProperty("proveedor")]
        public string proveedor { get; set; }
    }

    public class ArrTransport
    {
        [JsonProperty("id_transportistas")]
        public string id_transportista { get; set; }

        [JsonProperty("transportista")]
        public string transportista { get; set; }
    }

    public class ArrMultiple
    {
        [JsonProperty("arr_import_export")]
        public List<ArrImportExport> arr_import_export { get; set; }

        [JsonProperty("arr_proveedores")]
        public List<ArrProveedore> arr_proveedores { get; set; }

        [JsonProperty("arr_transportistas")]
        public List<ArrTransport> arr_transport { get; set; }
    }

    public class RootObject
    {
        [JsonProperty("arr_multiple")]
        public List<ArrMultiple> arr_multiple { get; set; }
    }

this is how a handle it in my main class

var resultado = await cliente.GetAsync(url);
var json = resultado.Content.ReadAsStringAsync().Result;


RootObject tr = JsonConvert.DeserializeObject<RootObject>(json);

                foreach (ArrMultiple arr in tr.arr_multiple) {
                    //await DisplayAlert("Array", arr.arr_import_export.ToString(), "Ok");

                    var arrClientes = new ObservableCollection<ArrImportExport>(arr.arr_import_export);
                    ListClientes.ItemsSource = arrClientes;

                    var arrProvedores = new ObservableCollection<ArrProveedore>(arr.arr_proveedores);
                    ListProveedores.ItemsSource = arrProvedores;

                    var arrTransportistas = new ObservableCollection<ArrTransport>(arr.arr_transport);
                    ListTransportistas.ItemsSource = arrTransportistas;
                }

I expect to see all my clients in one listview, all my suppliers in other listview, and carriers too. but I only see clients and get an exception error:

value cannot be null. parameter name: list

ListView in Xamarin.Forms is expected to have no other controls in its part of the layout (like in the StackLayout, in some Grid layouts it may work).

Possibly you could try to place this in the Grid with same row heights, it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM