如何在xamarin中使用NotifyDataSetChange?

时间:2016-09-18 17:20:03

标签: c# android listview xamarin

我是c#和xamarin的新手,我的排序有效,但我的Listview中的数据似乎没有受到影响。我尝试过使用NotifyDataSetChange,但似乎我没有使用它正确。

我的代码:

  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;
            }

EDITED ..

我的适配器中的更新方法

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

我如何调用我的适配器..

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

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

1 个答案:

答案 0 :(得分:1)

谢谢先生cheesebaron ..

在我的适配器上添加此方法解决了我的问题:)

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

并用它来调用我的适配器

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