Custom ListView not able to remove item from list

XamarinPMDXamarinPMD USMember ✭✭
edited June 2018 in Xamarin.Forms

I have two custom listView such as ListView A and ListView B. On ListView A contains the list of items when I click item in ListView A it will be highlighted(ie check and uncheck) on the specific item/data is populating into ListView B it is working fine. But when I uncheck item from ListView A and similar way trying to remove that item from ListView B it is not working.
While removing an item from custom ListView with check box not removing an item from ListView. but I am able to add that item but not able to remove.

onitemtap event of ListView control on the specific condition I am trying to Add or Remove the item from a list.

public class TestIds
 {
   ObservableCollection<ListViewModel> ListItems { get; set; } = new ObservableCollection<ListViewModel>()
     {
     };

  private void ListView_ItemTappedEventArgs(ItemTappedEventArgs itemTapped)
    {
       items = itemTapped.Item as ListViewModel;
       if (items.IsSelected)
          {
            items.IsSelected = false;
            BindRemoveItem(items.ID, items.IsSelected);
           }
           else
            {
             items.IsSelected = true;
             BindRemoveItem(items.ID, items.IsSelected);
             }
     }

    //this is my code for binding and removing data from listView

  private void BindRemoveItem(string id, bool isChecked)
    {
      IDManager.GetIDList(list =>
        {
         foreach (IDEntities item in list)
          {
           if (isChecked == true)
            {
             ListItems.Add(new ListViewModel { Text = item.IDDescription + " (" + item.IDCodes + ")", ID = item.IDCodeID });
             }
             else
             {
               ListItems.Remove(new ListViewModel { Text = item.IDDescription + " (" + item.IDCodes + ")", ID = item.IDCodeID }; 
    // here I am getting false
              }
            }
        CustomControlClass.ListView.ItemsSource = ListItems;
        }, id);
     }                  
}

Best Answer

  • XamarinPMDXamarinPMD US ✭✭
    Accepted Answer

    ListItems.Remove(ListItems.FirstOrDefault(i => i.ID == item.IDCodeID))

    this is the solution to remove listitem from ListView

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    You say this is with a CustomListView.
    Do you have the same problem with the standard out-of-the-box ListView?
    That at least tells you if you should be looking at your inherited custom control, or at your ViewModel code.

  • XamarinPMDXamarinPMD USMember ✭✭
    edited June 2018

    @ClintStLaurent said:
    You say this is with a CustomListView.
    Do you have the same problem with the standard out-of-the-box ListView?
    That at least tells you if you should be looking at your inherited custom control, or at your ViewModel code.

    I dont understand which is impacting the things in below comment I have added both viewmodal as wel as CustomListView

  • XamarinPMDXamarinPMD USMember ✭✭
    edited June 2018

    @ClintStLaurent
    this is my CustomControlClass class

     public class CustomControlClass: StackLayout
        {
    
            public ListView ListView { get; set; }
            public SearchBarRender barICDRender { get; set; }
    
            public CustomControlClass()
            {
                barICDRender = new SearchBarRender();
                ListView = new ListView();
                ListView.HasUnevenRows = true;
                Padding = new Thickness(10, 20);
                this.Children.Add(barICDRender);
                Children.Add(ListView);
            }
        }
    

    This is my View Modal class

     public class ListViewModel : INotifyPropertyChanged//BaseViewModel
        {
            public ListViewModel()
            {
            }
    
            string text;
            public string Text
            {
                get
                {
                    return text;
                }
                set
                {
                    text = value;
                    OnPropertyChanged(nameof(Text));
                }
            }
            string _id;
            public string ID
            {
                get
                {
                    return _id;
                }
                set
                {
                    if (_id != value)
                    {
                        _id = value;
                        // RaisePropertyChanged();
                        OnPropertyChanged(nameof(ID));
                    }
                }
            }
            bool _isLoading;
            public bool IsDataLoading
            {
                get
                {
                    return _isLoading;
                }
                set
                {
                    if (_isLoading != value)
                    {
                        _isLoading = value;
                        // RaisePropertyChanged();
                        OnPropertyChanged(nameof(IsDataLoading));
                    }
                }
            }
            bool isselected;
            public bool IsSelected
            {
                get
                {
                    return isselected;
                }
                set
                {
                    isselected = value;
                    OnPropertyChanged(nameof(IsSelected));
                    OnPropertyChanged(nameof(DetailImage));
                }
            }
    
            public Color TextColor
            {
                get
                {
                    return Color.Black;
                }
            }
    
            public string DetailImage
            {
                get
                {
                    if (IsSelected)
                    {
                        return "ic_checkmark_active";
                    }
                    return "ic_checkmark_unactive";
                }
            }
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected void OnPropertyChanged(string propertyName = "")
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    
  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Just some thoughts:
    public class ListViewModel
    You call it a ViewModel... But the properties read more like its a Model.
    It has an ID, IsSelected, and so on. Like it is one item that you would find in a collection.

    You're inheriting from StackLayout then adding UI controls through C# ?
    That's kinda odd. It means your custom control is really no longer a customized version of a StackLayout.

     public class CustomControlClass: StackLayout
        {
    
            public ListView ListView { get; set; }
            public SearchBarRender barICDRender { get; set; }
    

    Usually for something like that I'd make a ContentView with the ListView and SearchBar. Then you place that ContentView on your page. But as an inherited control is... not how any shop I've ever been a part of would approach it (or approve of).

    You're hard coding your colors?

            public Color TextColor
            {
                get
                {
                    return Color.Black;
                }
            }
    

    That totally kills any ability to theme or style in the future. What happens when the client says "We want it all to be in more soothing blues and greens" ? You have to scrub through thousands of lines, instead of just updating a single static resource or style definition.

    public PhysicianPatientICDProcedures()

    This is just a learning exercise, right? For yourself or for school, right? This isn't real production app that will really be used by a real physician. Right? :worried:

  • XamarinPMDXamarinPMD USMember ✭✭

    @ClintStLaurent said:
    Just some thoughts:
    public class ListViewModel
    You call it a ViewModel... But the properties read more like its a Model.
    It has an ID, IsSelected, and so on. Like it is one item that you would find in a collection.

    You're inheriting from StackLayout then adding UI controls through C# ?
    That's kinda odd. It means your custom control is really no longer a customized version of a StackLayout.

     public class CustomControlClass: StackLayout
        {
    
            public ListView ListView { get; set; }
            public SearchBarRender barICDRender { get; set; }
    

    Usually for something like that I'd make a ContentView with the ListView and SearchBar. Then you place that ContentView on your page. But as an inherited control is... not how any shop I've ever been a part of would approach it (or approve of).

    You're hard coding your colors?

            public Color TextColor
            {
                get
                {
                    return Color.Black;
                }
            }
    

    That totally kills any ability to theme or style in the future. What happens when the client says "We want it all to be in more soothing blues and greens" ? You have to scrub through thousands of lines, instead of just updating a single static resource or style definition.

    public PhysicianPatientICDProcedures()

    This is just a learning exercise, right? For yourself or for school, right? This isn't real production app that will really be used by a real physician. Right? :worried:

    I am doing it for my self. will you please suggest the solution to problem

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I am doing it for my self. will you please suggest the solution to problem

    I thought I just did. I wouldn't inherit from one control then add a bunch of controls. I'd make a ContentView that is laid out as expected.
    Here's an article explaining that further.

    I wouldn't hard-code colors like that. That's what styles and resources exist for.

  • XamarinPMDXamarinPMD USMember ✭✭
    Accepted Answer

    ListItems.Remove(ListItems.FirstOrDefault(i => i.ID == item.IDCodeID))

    this is the solution to remove listitem from ListView

Sign In or Register to comment.