Search code examples
androidlistviewxamarin.androidextend

Overriding ListView property in Monodroid


I'm currently programming in Monodroid and I'm having an issue with the extension of a Listview.

I currently have ListView extended like this:

public class TTListView : ListView
{
    private Context mContext;
    private bool wrapAdapter;

    public TTListView(Context context) :
        base(context)
    {
        Initialize();
        this.mContext = context;
    }

    public TTListView(Context context, IAttributeSet attrs) :
        base(context, attrs)
    {
        Initialize();
        this.mContext = context;
    }

    public TTListView(Context context, IAttributeSet attrs, int defStyle) :
        base(context, attrs, defStyle)
    {
        Initialize();
        this.mContext = context;
    }

    private void Initialize()
    {
        this.CacheColorHint = Color.Transparent;
        //Still some more stuff to be added here
    }

    public void InsertItemAt(int index)
    {
        Animation anim = AnimationUtils.LoadAnimation(
                 mContext, Resource.Animator.slide_top_down);
        anim.Duration = 500;
        this.GetChildAt(index).StartAnimation(anim);
    }

    public void SetDelegate(TTListDelegate _delegate)
    {
        this.OnItemClickListener = (IOnItemClickListener)_delegate;
        this.OnItemLongClickListener = (IOnItemLongClickListener)_delegate;
    }

    public override void AddFooterView(View v)
    {
        base.AddFooterView(v);
        wrapAdapter = true;
    }

    /*public override IListAdapter Adapter
    {
        get
        {
            return base.Adapter;
        }
        set
        {
            //Check if the passed parameter is a TTListAdapter
            TTListAdapter _ttadapter = value as TTListAdapter;
            if (_ttadapter != null)
            {
                _ttadapter.Wrapped = wrapAdapter;
            }
            base.Adapter = value;
        }
    }*/
}

The above code works perfectly fine. The problem with this is when I'm trying to override the Adapter property (which is now commented out) I get the following exception when trying to create a TTListView object:

"Unable to activate instance of type TimeTellApp.TTListView from native handle 40557188. No constructor found for TTListView::.ctor(System.IntPtr, Android.Runtime.JniHandleOwner)"

Usually this has something to do with the GC destroying the managed mapped object so up until now I solved these kind of problems by keeping a reference to the object. The problem with the TTListView is that the exception already comes up when calling the constructor for initialization.

I create a TTListView object like this:

TTListView setting_listview = new TTListView(this);

(Where this is a Activity) What could be the problem here and what would be the best way to solve it?


Solution

  • The error message says that you're missing a specific constructor in your class, which you should implement:

    protected ListView (IntPtr javaReference, Android.Runtime.JniHandleOwnership transfer)