Trouble updating ListView with Entry

Hi,

I am new to Xamarin Forms and I am having trouble updating a list item in a listview.
I get a list with value as zero and need to update the value before posting the list again.

The flow is:

  • Get List from GetAsync
  • Update ListView ItemsSource
  • Change Line Item
  • Post List with PostAsync

XAML

<ListView x:Name="DNLinesListView" Margin="0,10,0,0" HasUnevenRows="True" BackgroundColor="Transparent">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                </Grid.RowDefinitions>
                                <Label Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4" Text="{Binding Description}" TextColor="White" FontSize="Small" LineBreakMode="WordWrap" HorizontalOptions="Fill" />
                                <Label Grid.Row="1" Grid.Column="0" Text="Ordered:" TextColor="White" FontSize="Small" VerticalOptions="Center" />
                                <Label Grid.Row="1" Grid.Column="1" Text="{Binding Qty}" TextColor="White" FontSize="Small" VerticalOptions="Center" />
                                <Label Grid.Row="1" Grid.Column="2" Text="Received:" TextColor="White" FontSize="Small" VerticalOptions="Center" />
                                <Entry Grid.Row="1" Grid.Column="3" x:Name="QTY_REC" Text="{Binding QtyReceived}" TextColor="Black" FontSize="Small" VerticalOptions="Center" BackgroundColor="White" TextChanged="QtyChange" />
                            </Grid>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

XAML.CS

 public async void PopulateDNView(string item)
        {
            var client = new HttpClient();
            client.MaxResponseContentBufferSize = 256000;
            client.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);
            client.DefaultRequestHeaders.Add("Accept", "application/json");
            string userAndPasswordToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(username + ":" + password));
            client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", $"Basic {userAndPasswordToken}");

            var responseline = await client.GetAsync(Constants.DNLineUrl + "?PORef=" + item);

            var dnlineJson = await responseline.Content.ReadAsStringAsync();
            DeliveryNoteLines ObjDNLineList = new DeliveryNoteLines();
            if (dnlineJson != "")
            {
                ObjDNLineList = JsonConvert.DeserializeObject<DeliveryNoteLines>(dnlineJson);
            }
            DNLinesListView.ItemsSource = ObjDNLineList.DNLines;
            DNLineList = ObjDNLineList;
        }


        void QtyChange(object sender, TextChangedEventArgs e)
        {
            qty = e.NewTextValue;
            Console.WriteLine("value of: " + qty);
        }

        async void UpdateChangesProcedure()
        {
            var client = new HttpClient();
            client.MaxResponseContentBufferSize = 256000;
            client.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);
            client.DefaultRequestHeaders.Add("Accept", "application/json");
            string userAndPasswordToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(username + ":" + password));
            client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", $"Basic {userAndPasswordToken}");

            var jstring = JsonConvert.SerializeObject(DNLineList);
            var content = new StringContent(jstring, Encoding.UTF8, "application/json");
            var response = client.PostAsync(Constants.PostDeliveryUrl, content).Result;
            var result = JsonConvert.DeserializeObject<GeneralResponse>(response.Content.ReadAsStringAsync().Result);
            Console.WriteLine("Post Result: " + response.Content.ReadAsStringAsync().Result);

            if (result.Response == true)
            {
                await DisplayAlert("Uploaded", "Delivery Line changes have been uploaded to the server.", "Ok");
            }
            else
            {
                await DisplayAlert("Not Uploaded", "Changes has been uploaded to the server before.", "Ok");
            }

            await Navigation.PopModalAsync();

        }

Please let me know how to update the Line Item.

Thanks in advance,
Vince

Best Answer

Answers

  • Thanks ClintStLaurent, I have changed the code and implemented MVVM on the listview and can change and upload the values.

Sign In or Register to comment.