-2

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;
}
Anmol317
  • 1,356
  • 1
  • 14
  • 31

2 Answers2

1

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.

RajatN
  • 223
  • 1
  • 8
  • Is getActivity() in the android library or do I have to create it? –  Oct 27 '17 at 12:05
  • it is a method by which you can get the context of your activity in which the fragment is present, you don't have to write that method @BenjaminFillipucci. – RajatN Oct 27 '17 at 12:32
  • When I change (this) to (getActivity()), it tells me 'The name 'getActivity' does not exist in the current context.' –  Oct 27 '17 at 14:51
  • Try using this.Activity in place of getActivity() @Benjamin Fillipucci.It will work. check the edited answer. let me know in comments if the issue is resolved. – RajatN Oct 30 '17 at 04:50
  • The main thing is you have to pass the activity context. If you are building a native app then getActivity() is the method by which you can get the activity context but in xamarin there is no method like this, so you have to call this.Activity to get the activity context. – RajatN Oct 30 '17 at 04:54
0

var adapter = new jobsadapter(Activity);

  • I had to pass in the data as well. var JobsList = view.FindViewById(Resource.Id.TechsJob_List); JobsList.Adapter = new TechsJob_Adapter(this.Context, _TechsJob); –  Nov 07 '17 at 15:38