Search code examples
androidandroid-fragmentsxamarincustom-adapter

When creating a custom adapter can you only use it for an activity or can I use a fragment?


I am new to xamarin, and I am trying to create a list in a fragment and pass it to my main activity. but i am getting a null when i call my adapter. Here is my onCreateView in my fragment.

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{

  var view = inflater.Inflate(Resource.Layout.TechsJob_List, container, false);

  var JobsList = view.FindViewById(Resource.Id.TechListRow_ListView);

  var JobsListAdapter = new TechsJob_Adapter(this);

  JobsList.Adapter = JobsListAdapter; //NullRefernce

  var ignored = base.OnCreateView(inflater, container, savedInstanceState);

  base.OnCreateView(inflater, container, savedInstanceState);

  return view;
}

Solution

  • Your adapter needs the context not the fragement

    var JobsListAdapter = new TechsJob_Adapter(this);
    

    in this you are passing the fragement you should pass the activity context to it. Soo it should be like this

    var JobsListAdapter = new TechsJob_Adapter(this.Activity);
    

    Hope this will help you.