1

I wrote a custom ExpandableListView which Header is a Relative Layout with a child of TextView and Button.

In GetGroupView I define a btn.Click delegate which starts a new Activity:

      public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)

    {

        var view = convertView;

        var group = this.groups[groupPosition];

        if (view == null)

        {

            var inflater = context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;

            view = inflater.Inflate(Resource.Layout.ExpListViewItem, null);

        }

        view.FindViewById<TextView>(Resource.Id.expListViewItem_txt).Text = group.GetHeader();

        view.FindViewById<Button>(Resource.Id.expListViewItem_btn).Focusable = false;

        view.FindViewById<Button>(Resource.Id.expListViewItem_btn).Click += (object sender, EventArgs e) =>

        {

            var type = e.GetType();

            if(sender.GetType() == typeof(Button))

            {

            Intent temp = new Intent(context,typeof(TestActivity));

            temp.PutExtra("TestValue", group.GetHeader());

            context.StartActivity(typeof(TestActivity));

            }

        };

        return view;

    }

If I click on a ChildItem I want to start another Activity:

    public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)

    {

        var view = convertView;

        var group = this.groups[groupPosition];

        var item = group.GetItems()[childPosition];

        if (view == null)

        {

            var inflater = context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;

            view = inflater.Inflate(Resource.Layout.expListViewChildren, null);

        }

        view.FindViewById<TextView>(Resource.Id.expListViewChildren_txt).Text = item.GetHeader();

        view.Click += delegate  

        {

            Intent temp = new Intent(context, typeof(test));

            temp.PutExtra("TestValue", item.GetHeader());

            context.StartActivity(temp);

        };



        return view;
    }

If the ListView is collapsed, everything is fine but if its expanded, the Activity starts twice (due to the fact, that the btn.click delegate gets fired twice).

What am I missing here?

Thanks for your help!

user1021605
  • 241
  • 3
  • 14

1 Answers1

0

I ran into something very similar - make sure your delegate is not declaring two instances by being called twice. When the listview is expanded do you call the delegate declaration again?

The likelihood is there are two handlers out there and you need to find each and eliminate one of them.

Roy Hinkley
  • 10,111
  • 21
  • 80
  • 120
  • Log the declaration call - review your logs to see what is happening. – Roy Hinkley Mar 12 '14 at 20:08
  • @user1021605 Are you creating this list from different places? It sounds like it's being created under multiple context. – Roy Hinkley Mar 12 '14 at 21:00
  • So I checked again - following did I notice: As said, GetGroupView get called the Amount of Children in summed up in all Groups. Iterating through the first View, the View is null and get inflated. Iterating through the n+1 View View is NOT null, won't get inflated and hence the delegate get attached n+1 times. Since I am iterating through each Group several times, each Button get several delegates attached - so the main Problem ist - why the hell do getgroupview called the total of each Children? – user1021605 Mar 12 '14 at 21:20
  • How are you instantiating your listview implementation? Could you post your code? – Roy Hinkley Mar 12 '14 at 21:53
  • I uploaded a testapp: https://www.dropbox.com/sh/v5jbx3qv2e432j0/gOjFxeN680 If everything is collapsed and you hit Spoiler its working as intented. The second you expand one item and click on Spoiler, the Header will not be the one, you clicked on - so sth about following code seems to be messy view.FindViewById – user1021605 Mar 12 '14 at 21:54
  • @user1021605 One other thought - are you returning true from your handler onClick event? If not, perhaps the tap is cascading... I'll review the example app, but it may be a day or two - I don't know your urgency. You may also want to temporarily handle with a stop gap measure. Check out this post http://stackoverflow.com/questions/10943227/on-click-listener-called-twice – Roy Hinkley Mar 12 '14 at 22:06
  • Why shall i return true? http://developer.android.com/reference/android/view/View.OnClickListener.html#onClick(android.view.View) States, that the onclick event is a void method (well, its a handler, it never returns) – user1021605 Mar 12 '14 at 22:12
  • @user1021605 Sorry I was thinking of the onlongclick. Disregard. – Roy Hinkley Mar 12 '14 at 22:33
  • @user1021605 Check out this answer http://stackoverflow.com/questions/18376708/getview-called-multiple-times-in-android I have been slammed and have not had any time to review your sample. – Roy Hinkley Mar 25 '14 at 20:15