Using ArrayAdapter in an Android Fragment

MostafaElAwadyMostafaElAwady EGMember ✭✭
edited November 2014 in Xamarin.Android

I am trying to use an ArrayAdapter to populate a Spinner view located in a Fragment as follows

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    var view = inflater.Inflate(Resource.Layout.SalaryDefinition, container, false);

    spnBanks = view.FindViewById<Spinner>(Resource.Id.spinner1);

    spnBanks.Adapter = new ArrayAdapter<string>(CONTEXT, Resource.Layout.SalaryDefinition, Sections);

    return view;
}

The problem is that I couldn't find a context to pass to the ArrayAdapter constructor.

Then I was told that I can use this.Activity as cotext, but unfortunately when I tried this it crashed the application.

I also tried Application.Context and tried assigning the adapter at the OnActivityCreated event instead of the OnCreateView event, but it also crashed the app.

any idea where the problem is?

Posts

  • SebastianSeidel.9226SebastianSeidel.9226 DEInsider, University ✭✭✭✭

    Using an Activity property should be good to go if you create your adapter in OnActivityCreated.
    Can you provide the error you get if you create your adapter within OnActivityCreated?

  • KeyzerSKeyzerS NOMember ✭✭
    edited November 2014

    Hey!

    I think you need to do it in OnActivityCreated, like @SebastianSeidel‌ is suggesting, instead of in OnCreateView - where you currently have it? At least that is what I have done, I have not had any issues with this. I just pass the context like Activity:

    spinnerTypesAdapter = new ArrayAdapter(Activity, Android.Resource.Layout.SimpleSpinnerItem, typeOfProduct);

  • MostafaElAwadyMostafaElAwady EGMember ✭✭

    I already tried assigning the adapter at the OnActivityCreated event, as I mentioned in my original post, but it still have the same issue.

  • TroelsThistedTroelsThisted DKMember ✭✭
    edited November 2015

    Did you manage to solve this problem?? This is giving me some problems!

  • MostafaElAwadyMostafaElAwady EGMember ✭✭

    Nope, i had to skip using fragments completely because of this issue and resort to regular Activity class.

  • DaveYDaveY USUniversity ✭✭
    edited January 2016

    In your OnCreateView, do :

    context = container.Context;

  • Have you tried to create the adapter in the OnAttach(Context context) method? The method is called when the fragment and the activity are attached, so you shouldn't have a crash. I don't remember having such problem with Android SDK.

  • HadiSalehyHadiSalehy USMember ✭✭
    edited December 2017

    @MostafaElAwady I think I realized your problem :)
    Because you inherited from the fragment, you are not able to use the usual method.
    You will see a **...is not assignable to parameter to type "android.app.activity" ** error message.
    You should use the following method:
    spnBanks.Adapter = new ArrayAdapter<string>(this.Activity, , Resource.Layout.SalaryDefinition, Sections);

Sign In or Register to comment.