首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >listView itemSource表格更新

listView itemSource表格更新
EN

Stack Overflow用户
提问于 2015-01-09 09:58:01
回答 6查看 46K关注 0票数 12

好的,我有一个ListView对象,它的List<Filiale>作为ItemSource,每当对象列表发生变化时,我想刷新ItemSource。ListView有一个个性化的ItemTemplate,现在我已经这样做了:

代码语言:javascript
复制
public NearMe ()
{
    list=jM.ReadData ();
    listView.ItemsSource = list;
    listView.ItemTemplate = new DataTemplate(typeof(FilialeCell));
    searchBar = new SearchBar {
        Placeholder="Search"
    };
    searchBar.TextChanged += (sender, e) => {
        TextChanged(searchBar.Text);
    };
    var stack = new StackLayout { Spacing = 0 };
    stack.Children.Add (searchBar);
    stack.Children.Add (listView);
    Content = stack;
}

public void TextChanged(String text){
        //DOSOMETHING
        list=newList;
}

正如您在TextChanged方法中所看到的,我为前一个列表分配了一个新列表,但是视图中没有任何更改。在我创建的ViewCell中,我用SetBinding分配标签的文本字段

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2015-01-12 08:47:55

下面是我如何解决这个问题的方法,首先,我创建了一个“包装器”,它为我作为ItemSource使用的列表实现了ItemSource,如下所示:

代码语言:javascript
复制
public class Wrapper : INotifyPropertyChanged
    {
        List<Filiale> list;
        JsonManager jM = new JsonManager ();//retrieve the list

        public event PropertyChangedEventHandler PropertyChanged;
        public NearMeViewModel ()
        {
            list = (jM.ReadData ()).OrderBy (x => x.distanza).ToList();//initialize the list
        }

        public List<Filiale> List{ //Property that will be used to get and set the item
            get{ return list; }

            set{ 
                list = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, 
                        new PropertyChangedEventArgs("List"));// Throw!!
                }
            }
        }

        public void Reinitialize(){ // mymethod
            List = (jM.ReadData ()).OrderBy (x => x.distanza).ToList();
        }

然后在NearMe类中:

代码语言:javascript
复制
Wrapper nearMeVM = new Wrapper();
public NearMe ()
        {

            Binding myBinding = new Binding("List");
            myBinding.Source = nearMeVM;
            myBinding.Path ="List";
            myBinding.Mode = BindingMode.TwoWay;
            listView.SetBinding (ListView.ItemsSourceProperty, myBinding); 
            listView.ItemTemplate = new DataTemplate(typeof(FilialeCell));
            searchBar = new SearchBar {
                Placeholder="Search"
            };
            searchBar.TextChanged += (sender, e) => {
                TextChanged(searchBar.Text);
            };
            var stack = new StackLayout { Spacing = 0 };
            stack.Children.Add (searchBar);
            stack.Children.Add (listView);
            Content = stack;
        }
public void TextChanged(String text){
            if (!String.IsNullOrEmpty (text)) {
                text = text [0].ToString ().ToUpper () + text.Substring (1);
                var filterSedi = nearMeVM.List.Where (filiale => filiale.nome.Contains (text));
                var newList = filterSedi.ToList ();
                nearMeVM.List = newList.OrderBy (x => x.distanza).ToList ();
            } else {
                nearMeVM.Reinitialize ();
            }
票数 6
EN

Stack Overflow用户

发布于 2015-03-31 21:54:13

您可以将ItemsSource的ListView设置为null,然后再次将其设置为空,这样就可以重新加载表。http://forums.xamarin.com/discussion/18868/tableview-reloaddata-equivalent-for-listview

票数 20
EN

Stack Overflow用户

发布于 2015-01-09 10:24:47

将列表更改为ObservableCollection,并实现INotifyPropertyChanged以使更改反映在ListView中。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27857975

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档