locked
Problema em somar valores de um Listview Xamarin RRS feed

  • Question

  • User379132 posted

    Olá pessoal, estou com uma dificuldade em somar os valores exibido dentro de um ListView e exibir em uma Label;

    A Idea é bem simples, (exemplo) eu carrego 5 linhas do ListView com valores distintos (1 ,2, 3, 4, 5) e preciso obter a soma destes valores ( total 15 ) para informar numa Label, já utilizei um FOR mas o ListView não aceita listView.Items[i]; exatamente o ".Items".

    Alguém pode ajudar em obter essa soma?

    Obrigado!!

    Saturday, December 29, 2018 2:06 PM

Answers

  • User359363 posted

    First, please use English to describe your question.

    Second, you should do it in your view model. Here is an simple example:

    My View Model: ``` class Product { public string Name { get; set; } public double Number { get; set; } }

    class ProductVM : INotifyPropertyChanged
    {
        double _totalNumber;
        List<Product> _products;
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        public List<Product> Products
        {
            set
            {
                if (_products != value)
                {
                    _products = value;
                    OnPropertyChanged("Products");
                    Recalculate();
                }
            }
            get
            {
                return _products;
            }
        }
    
        public string Intro
        {
            get
            {
                return "Product Header";
            }
        }
    
        public double TotalNumber
        {
            set
            {
                if (_totalNumber != value)
                {
                    _totalNumber = value;
                    OnPropertyChanged("TotalNumber");
                }
            }
            get
            {
                return _totalNumber;
            }
        }
    
        void Recalculate()
        {
            foreach(Product price in Products)
            {
                this.TotalNumber += price.Number;
            }
            //.TotalPrice = this.Price * this.Quantity;
        }
    
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    

    ```

    ListViewPage.xaml:

    <ListView x:Name="listView" ItemsSource="{Binding Products}" Header="{Binding Intro}" Footer="{Binding TotalNumber}"> <ListView.HeaderTemplate > <DataTemplate> <StackLayout Orientation="Horizontal" Padding="10,5,5,10" BackgroundColor="Yellow"> <Label Text="~~"/> <Label Text="{Binding .}"/> <Label Text="~~"/> </StackLayout> </DataTemplate> </ListView.HeaderTemplate> <ListView.ItemTemplate> <DataTemplate> <TextCell Text="{Binding Name}" Detail="{Binding Number}"></TextCell> </DataTemplate> </ListView.ItemTemplate> </ListView>

    ListViewPage.xaml.cs: ``` public partial class ListViewPage : ContentPage { public ListViewPage() { InitializeComponent();

            var products = new List<Product> {
                new Product {Name = "Xander", Number = 1},
                new Product {Name = "Rupert", Number = 2},
                new Product {Name = "Tammy", Number = 3},
                new Product {Name = "Blue", Number = 4},
            };
    
            BindingContext = new ProductVM { Products = products };
        }
    }
    

    ```

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, December 31, 2018 9:07 AM

All replies

  • User359363 posted

    First, please use English to describe your question.

    Second, you should do it in your view model. Here is an simple example:

    My View Model: ``` class Product { public string Name { get; set; } public double Number { get; set; } }

    class ProductVM : INotifyPropertyChanged
    {
        double _totalNumber;
        List<Product> _products;
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        public List<Product> Products
        {
            set
            {
                if (_products != value)
                {
                    _products = value;
                    OnPropertyChanged("Products");
                    Recalculate();
                }
            }
            get
            {
                return _products;
            }
        }
    
        public string Intro
        {
            get
            {
                return "Product Header";
            }
        }
    
        public double TotalNumber
        {
            set
            {
                if (_totalNumber != value)
                {
                    _totalNumber = value;
                    OnPropertyChanged("TotalNumber");
                }
            }
            get
            {
                return _totalNumber;
            }
        }
    
        void Recalculate()
        {
            foreach(Product price in Products)
            {
                this.TotalNumber += price.Number;
            }
            //.TotalPrice = this.Price * this.Quantity;
        }
    
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    

    ```

    ListViewPage.xaml:

    <ListView x:Name="listView" ItemsSource="{Binding Products}" Header="{Binding Intro}" Footer="{Binding TotalNumber}"> <ListView.HeaderTemplate > <DataTemplate> <StackLayout Orientation="Horizontal" Padding="10,5,5,10" BackgroundColor="Yellow"> <Label Text="~~"/> <Label Text="{Binding .}"/> <Label Text="~~"/> </StackLayout> </DataTemplate> </ListView.HeaderTemplate> <ListView.ItemTemplate> <DataTemplate> <TextCell Text="{Binding Name}" Detail="{Binding Number}"></TextCell> </DataTemplate> </ListView.ItemTemplate> </ListView>

    ListViewPage.xaml.cs: ``` public partial class ListViewPage : ContentPage { public ListViewPage() { InitializeComponent();

            var products = new List<Product> {
                new Product {Name = "Xander", Number = 1},
                new Product {Name = "Rupert", Number = 2},
                new Product {Name = "Tammy", Number = 3},
                new Product {Name = "Blue", Number = 4},
            };
    
            BindingContext = new ProductVM { Products = products };
        }
    }
    

    ```

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, December 31, 2018 9:07 AM
  • User379132 posted

    @YorkGo,

    Muito obrigado pela ajuda, exatamente, isso deu certo!! Valeu amigo!

    Feliz 2019

    Tuesday, January 1, 2019 9:32 PM