Creating an (List) array of buttons with onclick events

ChristopherDrososChristopherDrosos GRMember ✭✭
edited August 2014 in Xamarin.Android

im trying to do something like this:

            Button button1 = view.FindViewById<Button> (Resource.Id.btnSE1);
            Button button2 = view.FindViewById<Button> (Resource.Id.btnSE2);

            List<Button> lstSEbuttons = new List<Button> {
                { button1 }
            };

            for (int i=0;i<lstSEbuttons.Count();i++){
                lstSEbuttons [i].Text = musicOptions.SoundEffectsLoadedItems [i].Name;
                lstSEbuttons [i].Click += (sender, e) => {
                    Console.WriteLine (i);
                    PlaySoundEffects(i);
                    };
                }

on the onclick event the value of i is 1, i would like for every button the value of i in PlaySoundEffects(i) to be for the first button 0 for the second 1 etc. How i can do that?

Posts

  • CheesebaronCheesebaron DKInsider, University mod
    edited August 2014

    The best way would probably be to save the i value in the Tag of the View and simply assign the same Click EventHandler for all of them like:

    private void ButtonOnClick(object sender, EventArgs eventArgs)
    {
        var button = sender as Button;
        if (button != null)
        {
            var index = (int)button.Tag;
    
            Console.WriteLine(index);
            PlaySoundEffects(index);
        }
    }
    

    and:

    var button1 = FindViewById<Button>(Resource.Id.button1);
    var button2 = FindViewById<Button>(Resource.Id.button2);
    var button3 = FindViewById<Button>(Resource.Id.button3);
    var button4 = FindViewById<Button>(Resource.Id.button4);
    
    var buttonsList = new List<Button>
    {
        button1,
        button2,
        button3,
        button4
    };
    
    for (var i = 0; i < buttonsList.Count; i++)
    {
        var button = buttonsList[i];
        button.Tag = i;
        button.Text = musicOptions.SoundEffectsLoadedItems[i].Name;
        button.Click += ButtonOnClick;
    }
    
  • ChristopherDrososChristopherDrosos GRMember ✭✭

    works great thanks

  • ChristopherDrososChristopherDrosos GRMember ✭✭
    edited August 2014

    Another way i have just found to do it also is this:

    Button btn1 = view.FindViewById<Button> (Resource.Id.btnSE1);
                Button btn2 = view.FindViewById<Button> (Resource.Id.btnSE2);
    
                var ListSEbuttons = new List<Button> {
                    { btn1 },
                    { btn2 },
                };
                for (int i=0;i<ListSEbuttons.Count();i++){
                    var button = ListSEbuttons[i];
                    button.Text = musicOptions.SoundEffectsLoadedItems [i].Name;
                    button.Tag = i;
                    int j = i;
                    button.Click += delegate {
                        Console.WriteLine ("List pos for sound effect: " + j);
                        PlaySoundEffects (j);
                    }; 
                }
    

    Is this solution less good in any way?

    Also i would like to have a List with Lists. (every list with have strings ints and i would like to be able to have all the lists in a array) is this approach too expensive and i will have problem with the memory or something? is it better if i have a list with the names of the lists and access them that way?

  • ChristopherDrososChristopherDrosos GRMember ✭✭
    edited August 2014

    In order to give a better understanding of what i need this is what im trying to do but thows errors:

    public List<MusicItem> Theme1 { get; set; }
    public List<List<MusicItem>> AllThemes { get; set; }
    
            AllThemes = new List<List<MusicItem>>
            {
                    Theme1 = new List<MusicItem>
                        {
                        new MusicItem(){ ThemeName = "Theme1",ThemeDesc = "Desc",Name = "..", Tension = "Low", ResId = Resource.Raw.test, LoopStart = 0},
                        new MusicItem(){ Name = "..", Tension = "Mid", ResId = Resource.Raw.test2, LoopStart = 0},
                        new MusicItem(){ Name = "..", Tension = "High", ResId = Resource.Raw.test2, LoopStart = 0},
                    };
            };
    
Sign In or Register to comment.