2
votes

Hi I am trying to update listView by adding and removing item from it. Adding Item in listview works fine in iOS and android well. But removing any item from it gives wrong out on android, it works perfect on iOS. In Android : whenever I delete a item from listview it deletes from collection but on UI it deletes last item present on UI after deleting when I add once again it adds previously deleted item into list not new new fresh item. Please check code I used

<ListView  RowHeight="100" HorizontalOptions="End" ItemsSource="{Binding GuidePrices, Mode=OneWay}" >
  <ListView.ItemTemplate>
    <DataTemplate>
      <ViewCell>
        <ViewCell.View>
          <StackLayout  Orientation="Horizontal">
            <controls:BindablePicker ItemsSource="{Binding Prices}" WidthRequest="150" SelectedItem="{Binding SelectedProduct, Mode=TwoWay}" />
            <Button Text="Add" IsVisible="{Binding Action}">
              <b:Interaction.Behaviors>
                <b:BehaviorCollection>
                  <b:EventToCommand  CommandNameContext="{b:RelativeContext CampsiteViewPage}"
                        EventName="Clicked" 
                        CommandName="AddPriceListCommand"
                        CommandParameter="{Binding}" /> 
                </b:BehaviorCollection>
              </b:Interaction.Behaviors>
            </Button>
             <Button Text="Delete" IsVisible="{Binding Action, Converter={StaticResource cnvInvert}}">
              <b:Interaction.Behaviors>
                <b:BehaviorCollection>
                  <b:EventToCommand  CommandNameContext="{b:RelativeContext CampsiteViewPage}"
                        EventName="Clicked" 
                        CommandName="DeletePriceListCommand"
                        CommandParameter="{Binding}" /> 
                </b:BehaviorCollection>
              </b:Interaction.Behaviors>
            </Button>
          </StackLayout>
        </ViewCell.View>
      </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

--ViewModel code for on add and delete command

this.AddPriceListCommand = new Command((s) =>
        {
            if (_guidePrices != null)
                {
                _guidePrices.Add(new PricesListModel() { ListId = Guid.NewGuid(), Action = false, Prices = _campsite.Guides[0].Prices });
                }
        });

        this.DeletePriceListCommand = new Command((sender) => {

        var  _priseList = (from priceList in _guidePrices
                           where priceList == (PricesListModel)sender
                   select priceList).ToList();
        _guidePrices.RemoveAt(_guidePrices.IndexOf((PricesListModel)sender));

            });

I am not able find cause why this behave on android as same code works in ios as expected.

1
What Xamarin.Forms version are you using? - Prashant Cholachagudda
here is my VS configuration of xamarin Xamarin 3.9.519.0 (5667ef4) Xamarin.Android 4.20.0.37 Xamarin.iOS 8.8.1.0 Xamarin.iOS Unified Migration 1.0 - Rajendra

1 Answers

-1
votes

What is the type of _guidePrices? Removing an item from a List<T> does not update the UI. You need to use the ObservableCollction<T> or raise PropertyChanged event notify UI to update