ListView not show item after filter textsearch ?

SiwakornApichitsopaSiwakornApichitsopa USMember ✭✭

I don't use xaml file. I generate code in popuppage class.

public class WrappedItemSelectionTemplate : ViewCell
        {
            public WrappedItemSelectionTemplate() : base()
            {
                Label name = new Label();

                name.SetValue(Label.TextColorProperty,Color.FromHex(SetAllPage.TextListColor));
                name.SetBinding(Label.TextProperty, new Binding("ITEMS.ListDescription"));

                Switch mainSwitch = new Switch();
                mainSwitch.SetBinding(Switch.IsToggledProperty, new Binding("IsSelected"));

                RelativeLayout layout = new RelativeLayout( );
                layout.BackgroundColor = Color.FromHex(SetAllPage.BackGroundList);

                layout.Children.Add(name,
                    Constraint.Constant(5),
                    Constraint.Constant(5),
                    Constraint.RelativeToParent(p => p.Width - 60),
                    Constraint.RelativeToParent(p => p.Height - 10)
                );

                layout.Children.Add(mainSwitch,
                    Constraint.RelativeToParent(p => p.Width - 55),
                    Constraint.Constant(5),
                    Constraint.Constant(50),
                    Constraint.RelativeToParent(p => p.Height - 10)
                );
                View = layout;
            }
        }

        public List<WrappedSelection<T>> WrappedItems = new List<WrappedSelection<T>>();

        public void reBindList(List<T> items)
        {
            WrappedItems = items.Select(item => new WrappedSelection<T>() { ITEMS = item, IsSelected = false }).ToList();
            mainList = new CustomListView()
            {
                ItemsSource = WrappedItems,
                ItemTemplate = new DataTemplate(typeof(WrappedItemSelectionTemplate)),
                IsRefreshing = true,
                IsPullToRefreshEnabled = false,
                MinimumHeightRequest = 50,
                HeightRequest = 150,
            };

            mainList.ItemSelected += (sender, e) => {
                if (e.SelectedItem == null) return;
                var o = (WrappedSelection<CheckListItem>)e.SelectedItem;
                o.IsSelected = !o.IsSelected;
                ((CustomListView)sender).SelectedItem = null; //de-select
            };
        }

        public ListViewPageForParam(List<T> items,bool isShowSearchbar, List<CheckListItem> t)
        {
            _items = t;           
            reBindList(items);

            // Display SearchBar
            StackLayout sbar;
            if (isShowSearchbar == true)
            {
                sbar  = new StackLayout { Padding = new Thickness(10, 10, 10, 10), Children = { GetViewSearchBar(), mainList } };
            }
            else
            {
                sbar = new StackLayout { Padding = new Thickness(10, 10, 10, 10), Children = { mainList } };
            }

            Content = sbar;
        }
public List<T> GetSelection()
        {
            return WrappedItems.Where(item => item.IsSelected).Select(wrappedItem => wrappedItem.ITEMS).ToList();
        }


        private CustomSearchBar GetViewSearchBar()
        {
            sbar = new CustomSearchBar
            {
                Placeholder = "Enter Text",
                HeightRequest = 70,
                WidthRequest = 70
            };
            sbar.TextChanged += (sender, args) => SearchBar_OnTextChanged(sender, args);
            sbar.SearchButtonPressed += (sender, args) => SearchBar_OnButtonPressed(sender, args);
            return sbar;
        }

        private void FilterItmes()
        {
            var lvList = _items.ToList();

            string e = sbar.Text.ToString();

            mainList.BeginRefresh();

            if (string.IsNullOrWhiteSpace(e.ToString()))
            {
                lvList = _items.ToList();
            }
            else
            {
                lvList = _items.Where(i => i.ListDescription.Contains(e.ToLower())).ToList();
            }

            mainList.ItemsSource = null;
            mainList.ItemTemplate = null;

            reBindList(lvList.Cast<T>().ToList());
            mainList.EndRefresh();
        }
        private void SearchBar_OnTextChanged(object sender, TextChangedEventArgs e)
        {
            FilterItmes();      
        }

        private void SearchBar_OnButtonPressed(object sender, EventArgs args)
        {
            FilterItmes();
        }

Answers

  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai

    @SiwakornApichitsopa,

    I don't think you want to set the ItemTemplate to null. Remove this line in the FilterItems method.

    mainList.ItemTemplate = null;
    
  • Thank @John Miller

    I has recreate itemtemplate again with rebindlist function.

    Main problem when I entry text in textsearch. I debug my list item. It has item.
    But textcolor has reset to same background color.But I can make sure.
    TextColor and BackgroundColor I set difference color.

  • Dear All,

    I know problem. It has 2 point.
    1.) ListView not automatic refresh if Item data has changed.
    2.) Content Page don't refresh If I regen itemsource or itemtemplate.

    It work for me.
    If I reassign class but I don't like this solution because I think it not
    make sense about refresh view.

Sign In or Register to comment.