Monodroid - Passing Data To Listview According To Gref Limit
C#, Mono for Android. I need to output a large portion of combined data into ListView. To achieve this, I use the following obvious approach with Adapter: class ItemInfo { publ
Solution 1:
In my project i had the same problem.
- ItemInfo is managed object, so you don't need to do anything with it, GC collects when it will be necessary.
- ListView does not load view for each list item at once, so you can control number of created views and dispose them.
Here is my solution
I have removed some unnecessary overrides from BaseAdapter so don't be afraid if it ask you to implement them.
classCustomViewAdapter : BaseAdapter<ItemInfo>
{
privatereadonly Context _context;
private IList<ItemInfo> _items;
privatereadonly IList<View> _views = new List<View>();
publicCustomViewAdapter(IntPtr handle)
: base(handle)
{
}
publicCustomViewAdapter (Context context, IList<ItemInfo> objects)
{
_context = context;
_items = objects;
}
privatevoidClearViews()
{
foreach (var view in _views)
{
view.Dispose();
}
_views.Clear();
}
publicoverride View GetView(int position, View convertView, ViewGroup parent)
{
var inflater = LayoutInflater.From(_context);
var row = convertView ?? inflater.Inflate(Resource.Layout.ListItemView, parent, false);
/// set view dataif(!_views.Contains(row))
_views.Add(row);
return row;
}
publicoverrideint Count
{
get { return _items == null ? 0 : _items.Count; }
}
publicoverridevoidDispose()
{
ClearViews();
base.Dispose();
}
}
Usage example
[Activity]
publicclassMessagesActivity : Activity
{
private CustomViewAdapter _adapter;
protectedoverridevoidOnCreate(Android.OS.Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.ListView);
_adapter=new CustomViewAdapter(this,Enumerable.Empty<ItemInfo>);
FindViewById<ListView>(Resource.Id.list).Adapter=_adapter;
}
publicoverridevoidFinish()
{
base.Finish();
if(_adapter!=null)
_adapter.Dispose();
_adapter=null;
}
}
Solution 2:
Answer found. I've inherited my ItemInfo class from Java.Lang.Object explicitly and now can call Dispose() when object is no longer needed. This kills leaking GREFs.
Post a Comment for "Monodroid - Passing Data To Listview According To Gref Limit"