0
votes

I have a listview with data from a list called actualmeterreading. In each row, there are a label and entry, both display data from the list. User can edit the data displayed in entry. How can I loop through the listview and get the data that user input in the entry?

Code for populating the listview.

 list = new ListView
 {
      ItemsSource = actualmeterreading,
      RowHeight = 50,

      ItemTemplate = new DataTemplate(() =>
      {
           printDesc = new Label();
           string desc = actualmeterreading[x].MachineMeterReadingList.Meterreadingdescription.Description;
           printDesc.Text = desc;
           meterreading = new Entry();
           string reading = actualmeterreading[x].ActualReading.ToString();
                            meterreading.Text = reading;
                            meterreading.FontSize = 14;

            x = x + 1;

            //nameLabel2.WidthRequest = 300;
            //nameLabel2.SetBinding(Entry.TextProperty, "");
            // Return an assembled ViewCell.

            return new ViewCell
            {
                   View = new StackLayout
                   {
                          Padding = new Thickness(0, 5),
                          Orientation = StackOrientation.Horizontal,
                          Children =
                          {
                              new StackLayout
                              {
                                   VerticalOptions = LayoutOptions.Center,
                                   Spacing = 0,
                                   Children =
                                   {
                                        printDesc,
                                        meterreading
                                        //nameLabel2

                                   }
                                }
                           }
                    }
           };
      })
};

Loop through the listview on submit button click.

    private void submitbtn_Clicked(object sender, EventArgs e)
    {
        int x = 0;
        foreach (var c in  list.ItemsSource)
        {
            int r = c.?
            myactualreading = new actualmeterreading
            {
                ActualReading = r
            };
            x = x + 1;
            dataservice.UpdateActualReading(myactualreading);
        }

    }

When I did some search, there was someone who mention to View models and two ways bindings. Does anyone have some solution regarding that one or any other solutions? Thank you

1
the best way is to do with MVVM databinding, if you are not willing to go that way, just write a Text changed event for Entry and update source item thereKrishna

1 Answers

0
votes

Refer the below code to achieve your requirement. Whenever the entry is edited, then it will be stored in model and label also will get reflected.

         <ListView ItemsSource="{Binding Elements}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition />
                                <ColumnDefinition />
                            </Grid.ColumnDefinitions>
                            <Entry Grid.Column="0" Text="{Binding Name,Mode=TwoWay}" />
                            <Label Grid.Column="1" Text="{Binding Name,Mode=TwoWay}" />
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>


    public partial class MainPage : ContentPage
    {
        ObservableCollection<Model> elements; 
        public ObservableCollection<Model> Elements
        {
           get { return elements;}
           set { elements = value;}
        }


        public MainPage()
        {
            Elements = new ObservableCollection<Model>() { new Model() { Name = "One" }, new Model() { Name = "Two" } };
            BindingContext = this;
            InitializeComponent();
        }
    }

    public class Model : INotifyPropertyChanged
    {
        string name;
        public string Name
        {
            get { return name; }
            set {
              name = value;
                NotifyPropertyChanged("Name");
            }
        }

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }