android如何在 xamarin 中使用 NotifyDataSetChange?

标签: listview Android C#
发布时间: 2016/10/10 21:46:08
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我是新到 c# 和 xamarin,像我这种工作中的数据但我 Listview 看来不会受到影响。我试过使用 NotifyDataSetChange,但它看起来像 correcty 拿不了它。

我的代码︰

  public override bool OnOptionsItemSelected(IMenuItem item)
        {
            List<Mountain> filteredMountains;

            int id = item.ItemId;
            if (id == Resource.Id.srtAtoZ)
            {
                filteredMountains = (mMountains.OrderBy(mountain => mountain.MtName)).ToList();
                mAdapter = new MountainsAdapter(this, Resource.Layout.ml_model, filteredMountains);
                mAdapter.NotifyDataSetChanged();                
                mListView.Adapter = mAdapter;

            }
            else if (id == Resource.Id.srtZtoA)
            {
                filteredMountains = (mMountains.OrderByDescending(mountain => mountain.MtName).ToList());
                mAdapter = new MountainsAdapter(this, Resource.Layout.ml_model, filteredMountains);
                mAdapter.NotifyDataSetChanged();                
                mListView.Adapter = mAdapter;
            }
      else if (id == Resource.Id.srtMaslAsc)
            {
                filteredMountains = (mMountains.OrderBy(mountain => mountain.Masl)).ToList();
                mAdapter = new MountainsAdapter(this, Resource.Layout.ml_model, filteredMountains);
                mAdapter.NotifyDataSetChanged();
                mListView.Adapter = mAdapter;
            }
                 else if (id == Resource.Id.srtMaslDesc)
            {
                filteredMountains = (mMountains.OrderByDescending(mountain => mountain.Masl)).ToList();
                mAdapter = new MountainsAdapter(this, Resource.Layout.ml_model, filteredMountains);
                mListView.Adapter = mAdapter;
            }

编辑。

在我更新方法我adapter

public  void Update(List<Mountain> mLists)
      {
          mtList.Clear();
          NotifyDataSetChanged();
      }

我怎么叫我 adapter

    else if (id == Resource.Id.action2)
        {
            List<Mountain> filteredMountains = (mMountains.OrderByDescending(mountain => mountain.MtName).ToList());

            mAdapter.Update(filteredMountains);
            RunOnUiThread(() => mAdapter.NotifyDataSetChanged());

解决方法 1:

谢谢主席先生 cheesebaron。

添加此方法对我 adapter :) 解决我的问题

public  void Update(List<Mountain> mLists)
    {
        mtList.Clear();
        mtList.AddRange(mLists);
        NotifyDataSetChanged();
    }

用这个,调用我adapter

mAdapter.Update(filteredMountains);
RunOnUiThread(() => mAdapter.NotifyDataSetChanged()); 
官方微信
官方QQ群
31647020