Reputation: 262
I am developing an app in Xamarin.Android. Here, I am using listview with baseadapter to append the list of contacts. Now I need to append items once scrolled to end of the listview from database. I mean the concept is like "Load More Items". At first I add the items to adapter by re-creating it whenever scrolled to end of the listview. But I came to know that it is not optimal way from the link Load More Item in lazy adapter and listview . My code is as below,
allContactsList -> getting items from database
adapter -> My custom adapter
if (allContactsList != null && allContactsList.Count() > 0)
{
if(adapter == null)
adapter = new AllContactsListAdapter(this, allContactsList);
else
{
//Stucks here. I can't append to adapter
}
adapter.NotifyDataSetChanged();
listView.Adapter = adapter;
}
How can I append new items to an adapter in Xamarin.Android?
Upvotes: 1
Views: 1050
Reputation: 262
My working code is as below.
List<MyClass> existingAllContactsList;
MyAdapter adapter;
ListView listView;
protected override void OnCreate(Bundle savedInstanceState)
{
...
...
BindData();
}
void BindData()
{
var allContactList = list of items // from database
if (allContactList != null && allContactList .Count() > 0)
{
if (adapter == null)
{
existingAllContactsList= allContactList;
adapter = new MyAdapter(this, existingAllContactsList);
listView.Adapter = adapter;
}
else
{
if (existingAllContactsList!= null && existingAllContactsList.Count() > 0)
existingAllContactsList.AddRange(allContactList);
else
existingAllContactsList= allContactList;
}
adapter.NotifyDataSetChanged();
}
}
Upvotes: 1
Reputation: 116
Listview is kind of deprecated and not be used anymore in professional apps. Use Recyclerview from support library instead. It's more optimal in loading items and has more features. The structure of loading items is almost the same. When you want to add items to an existing list, just add them to your content list and call NotifyDataSetChanged();
after that, as @Joe Lv - MSFT said.
Upvotes: 1