Forum Xamarin.Android

How to reach a adapter object with eventArgs?

Im like doing this to a imageView i have in my adapter

pokemon.Click += clicker;

( i passed clicker through parameter to the adapter as an EventHandler )

and in my main activity i have the

public void clicker(object obj,EventArgs args){
((View)obj).FindViewById<ImageView> (Resource.Id.pokemon).Visibility = ViewStates.Invisible;
}

but i need to change the visibility of another imageView that is in my parameter it is named digimon, but if i do this

public void clicker(object obj,EventArgs args){
    ((View)obj).FindViewById<ImageView> (Resource.Id.pokemon).Visibility = ViewStates.Invisible;
    ((View)obj).FindViewById<ImageView> (Resource.Id.digimon).Visibility = ViewStates.Invisible;
}

i get "Object reference not set to an instance of an object"

How can i reach that second image? help please

Posts

  • CheesebaronCheesebaron DKInsider, University mod

    obj is the ImageView, so you need to drill upwards in the View hierarchy to get the parent of the other ImageView.

    Imagine the hierarchy is like this:

    LinearLayout
        |
        |----> ImageView (pokemon)
        |
        |----> ImageView (digimon)
    

    Now when using

    ((View)obj).FindViewById<ImageView>(Resource.Id.pokemon)
    

    This should work fine, because obj is already the ImageView you attached the clicker event to. In this case pokemon. So you could just do: ((ImageView)obj).Visibility = ....

    If you want to find digimon in there as well you will need to do something like:

    var linearLayout = ((View)obj).Parent;
    

    This will get you the root of the hierarchy, which in this case is the LinearLayout and on that call FindViewById<ImageView>(Resource.Id.digimon).

    However, I prefer just to do it on the contentView instead and simply call FindViewById<ViewType>() on it rather than doing it on the object passed to the EventHandler.

  • HiltonMenezesHiltonMenezes BRMember ✭✭

    Wokred fine, but how can i use this contentView?

  • CheesebaronCheesebaron DKInsider, University mod

    It is what you set at the beginning of your Activity's OnCreate method, with SetContentView. Then that should be available in your Activity by simply calling FindViewById().

Sign In or Register to comment.