Reputation: 1011
here is my code:
public class ClientsFragment : ListFragment
{
List<ClientViewModel> list;
Timer timer;
public override async void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
await LoadClientList();
ListAdapter = new ClientAdapter(Activity, list);
timer = new Timer(5000);
timer.Elapsed += Timer_Elapsed;
timer.Enabled = true;
}
private async void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
await LoadClientList();
}
public async Task LoadClientList()
{
list = await ClientViewModel.GetAllClients();
}
}
Everything works fine, except updating content. I have read, that NotifyDataSetChanged function is needed, but I don't know how to implement that, because every example found on web was connected with Activity or custom ListView, not ListFragment. I don't know if my timer is well-written too. Thanks for help
EDIT:
Solution was to use Activity.RunOnUiThread(() => { ListAdapter = new ClientAdapter(Activity, list); });
in the bottom of LoadClientList
method.
Upvotes: 0
Views: 391
Reputation: 13176
Basically if you want to reset the content in your fragment, you will need to re-attach your adapter in your scenario.
Ideally you would call NotifyDataSetChanged
once you do something to the Adapter
such as Add()
, Remove()
, Insert()
, Clear()
, etc.
So there's a few ways you can approach this:
Simply re-create the Adapter with the new list that contains the updated list.
Within your Custom Adapter
, simply allow yourself to change the List
inside which might call to re-attach the Adapter or call NotifyDataSetChanged
using the respective methods above.
Note: You may want to ensure you are calling NotifyDataSetChanged
on the UI thread.
Upvotes: 1