Getwrong
Getwrong

Reputation: 187

Passed data not showing in listView

i am trying to pass data from a child page to to a parent class this is the parent

namespace App11
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class OrderedItemsPage : ContentPage
    {
        public string aSize;
        public string size, Main, Milk;
        public List<String> sizeList = new List<string>();
        public List<String> mainList = new List<string>();
        public List<String> milkList = new List<string>();
        public ObservableCollection<ElementsViewModel> elements { get; set; }
        public void OrderedItemsPageBlank()
        {
            aSize = "";
            Main = "";
            Milk = "";
        }
        public OrderedItemsPage ()
        {
            InitializeComponent ();

        }
        public void displayToList()
        {
            elements = new ObservableCollection<ElementsViewModel>();
            for(int i = 0;i< mainList.Count;i++)
            {
                string Coffee = sizeList[i] + " : " + mainList[i];
                elements.Add(new ElementsViewModel
                {
                    Image = "icon.png",
                    Coffee = Coffee,
                    Details = "$2.50 each"
                });
            }

            listView.ItemsSource = elements;
        }
    }
}

and this is the child class...

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="App11.OrderedItemsPage">
<ContentPage.Content>
    <StackLayout >
        <!--Top Section-->
        <ListView  x:Name="listView" RowHeight="60">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout
                    Orientation="Horizontal"
                            HorizontalOptions="Fill">
                            <Image x:Name="BgImage" Source="{Binding Image}"
                                   AbsoluteLayout.LayoutBounds="250.25, 0.25, 50, 50"/>
                            <StackLayout Orientation="Vertical">
                                <Label Text="{Binding Coffee}"
                                       FontSize="24"
                                       AbsoluteLayout.LayoutBounds="0.25, 0.25, 400, 40"
                                ></Label>
                                <Label Text="{Binding Details}"
                                       AbsoluteLayout.LayoutBounds="50, 50, 200, 25"
                                ></Label>
                            </StackLayout>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

        <StackLayout Orientation="Horizontal">
            <!--Second half-->
            <Button
                x:Name="AddButton"
                Text="Add Drink"
                    VerticalOptions="EndAndExpand"
                    HorizontalOptions="CenterAndExpand"
                    Clicked="addDrinkButtonClick">

            </Button>
            <Button Text="Add Food"
                    VerticalOptions="EndAndExpand"
                    HorizontalOptions="CenterAndExpand">

            </Button>
        </StackLayout>
            <!--third half ( cause thats a thing)-->
            <StackLayout Orientation="Vertical">
                <Button Text="Process Order"
                    VerticalOptions="EndAndExpand"
                    HorizontalOptions="CenterAndExpand"
            ></Button>
            </StackLayout>

    </StackLayout>
</ContentPage.Content>

i basically need to receive the data, now when i debug the program the information is passed through perfect but when i populate is into the list view it does not display when i run the app?

any help would be awesome, thanks

Upvotes: 0

Views: 141

Answers (1)

Gerald Versluis
Gerald Versluis

Reputation: 34128

This probably is the culprit: elements = new ObservableCollection<ElementsViewModel>();.

Do not new a ObservableCollection every time. This will cause the binding to updating the UI and your changes will not show up. Just instantiate the elements once: public ObservableCollection<ElementsViewModel> elements { get; set; } = new ObservableCollection<ElementsViewModel>(); and clear it every time you need an empty one. Then just add items to it one-by-one.

A helpful package here is the MVVM Helpers by James Montemagno. It has the ObservableRangeCollection, which lets you replace whole ranges and such, saving you some foreaches.

Upvotes: 1

Related Questions