list view button image change in load

ShamnadShamnad USMember ✭✭

Hi friends , in my app i have a cart option, items are adding to cart is from a list view , also cart is other list view. While i add item to cart the i can change the image. Initially the button image is + and it will change to - i fond this code from https://forums.xamarin.com/discussion/comment/156279/#Comment_156279 . my problem is while i came back to the page all button changes to + .
I need to bind the button image inside list view according to a condition i,e if the value exist in the cart the image should me - else image should be - .
This problem was discussed early in forms and i got some Knowles from that discussion
my button code

btn_Cart = new ImageButton{ BackgroundColor = Color.Transparent, VerticalOptions = LayoutOptions.CenterAndExpand, HorizontalOptions = LayoutOptions.CenterAndExpand, }; var addedOrNotImageBinding = new Binding("IsAddedToCart", 0, new ImagePathConvertor()); btn_Cart.SetBinding(ImageButton.SourceProperty, addedOrNotImageBinding);

ImagePathConvertor

public class ImagePathConvertor : IValueConverter{ public ImagePathConvertor() { } #region IValueConverter implementation public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ((bool)value) { return "minus"; } else{ return "add"; } } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture){ throw new NotImplementedException(); } #endregion }

you can see the first image which i add some items to cart added item is change to - other remain to + but if i go back and came again to this page the image all + like the second image

Posts

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    @Shamnad can you attach a working example project so I can debug it?

  • ShamnadShamnad USMember ✭✭

    @AlessandroCaliaro sorry for the late replay, as per your request i'm attaching a sample her please find it from here .

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I'll take a look later

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Hi @Shamnad .
    I have take a look to your App (I have also written an app for restaurant... so I know problems..)
    I think you are using a "viewModel" for your "Menu" (where you receive 5 items: Starter1, 2, ...)
    Then a Temp viewModel where you have "Ordered" starter.
    When you press "+", you change "IsAdded..." property.
    I think that the problem is when you press "Starter" button (in first menu where there are Starter, Snack....).
    You "reload" all viewModel, and in this situation "IsAdded..." property is always false.
    When you have finished to load the viewModel:

            var client = new HttpClient();
                var json = await client.GetStringAsync(string.Format(Url));
                var items = JsonConvert.DeserializeObject<ObservableCollection<cm_items>>(json.ToString());
                foreach (var item in items)
                {
    
                    ListItems.Add(item);
    
                    IsBusy = false;
                }
    

    I think you have to verify if items present in ListItems are present also in your Tmp array

    something like

               var json = await client.GetStringAsync(string.Format(Url));
                var items = JsonConvert.DeserializeObject<ObservableCollection<cm_items>>(json.ToString());
                foreach (var item in items)
                {
                    for (int i = 0; i < App.Instance.ViewModel.TempList.Count; i++)
                    {
                        if (App.Instance.ViewModel.TempList[i].item_id == item.item_id)
                        {
                            //blInsertStatus = false;
                            item.IsAddedToCart = true;
                            //App.Instance.ViewModel.TempList.Remove(BindingContext as cm_items);
                            break;
                            //var s = BindingContext;
                            //App.Instance.ViewModel.TempList.Remove(s as cm_items);
                        }
                    }
    
                    ListItems.Add(item);
    
                    IsBusy = false;
                }
    
  • ShamnadShamnad USMember ✭✭
    edited December 2015

    @AlessandroCaliaro it works nicely thank you very much :smile:

  • ShamnadShamnad USMember ✭✭
    edited December 2015

    @AlessandroCaliaro i fount other problem there i cant remove an item after navigate back to from cart page
    also i have duplication problem i have tried but not resolved .

    btn_Cart.Clicked += (sender, e) =>{ sender = BindingContext; cm_items item = (cm_items)sender; Int32 inItemId = (BindingContext as cm_items).item_id; var count = App.Instance.ViewModel.TempList.Count; var value = App.Instance.ViewModel.TempList; Boolean blInsertStatus = true; try{ if (count == 0) { item.IsAddedToCart = true; value.Add(item); } else { for (int i = 0; i < count; i++) { if (value [i].item_id == inItemId) { value.Remove(item); blInsertStatus = false; item.IsAddedToCart = false; break; } } if (blInsertStatus) { if (item.IsAddedToCart) { item.IsAddedToCart = false; value.Remove(item); } else { item.IsAddedToCart = true; value.Add(item); } } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); throw; } };

    i'm sorry to asking again and again . when i removing an item on first time there is no problem but after navigate to different page it make's trouble. item is not removing and image not change . what is the problem with my code

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I think you should use two buttons, one with plus and on with minus. If you press plus , the first time you add a record to your temp list, otherwise you add 1 to a counter. If you press minus you decrement the counter or, if the counter is 1, you remove the record from your list temp

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    But this is not relative to Xamarin, this is an implementation problem.

  • ShamnadShamnad USMember ✭✭
    edited December 2015

    @AlessandroCaliaro i can attach a debug video that will demonstrate my actual problem i think i don't need two button for this any how i'm now doing the counter option as you mention in the previous post

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I think you need two buttons because if I Bret to order two beers I don't understand how to do.

    I think you don't remove items from your temp list. Try to debug the 'count ' variable after removed all items. It's 0?

  • ShamnadShamnad USMember ✭✭

    @AlessandroCaliaro But on first time it is adding and removing the problem comes only after navigating to previous pages . Any how there is a solution with two button :smile: i can implement that .

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Two buttons aren't a solution for the problem but I think your app should use two buttons. Have you take a look to Count variable?

  • ShamnadShamnad USMember ✭✭
    edited December 2015

    @AlessandroCaliaro i will do it and in inform you, thanks for helping me . by counter you mean ones i click the button counter will get the value 1 and add the item. again i click it will check the counter and if it is 1 the item will remove else item will add . is this the logic you mentioned ?

Sign In or Register to comment.