Merbs Anda Merbs Anda - 2 months ago 24
Android Question

how to use NotifyDataSetChange in xamarin?

I'm new to c# and xamarin, my sort works but the data in my Listview doesn't seems to be affected. I've tried using NotifyDataSetChange but it seems like I'm not using it correcty.

my code:

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..

my update method in my adapter

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


how I call my adapter..

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

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

Answer

thank you sir cheesebaron..

adding this method on my adapter solved my problem :)

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

and used this to call my adapter

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