Need help posting checkboxes to another activity page when checked!!!

I am a new developer and I am currently working on an app for android in Xamarin that has users selecting phrases via a checkbox. When the checkbox is selected the user needs to be able to press a button that will take the user to a results page. Here in the results page all of the checked checkboxes will appear as a checkbox. I have the code written out for most of this but I cannot figure out how to post the checkboxes to results page. I was looking into intents and I am not sure if I am using them right. Here is the code I have for one of the checkboxes.

button0.Click += delegate {
StartActivity(typeof(MainActivity));
};
//Get Results button
Button button01 = FindViewById (Resource.Id.button01);
button01.Click += delegate {
if (cb0.Checked) {
var intent = new Intent(this, typeof(results));
StartActivity(intent);

Posts

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

    I think there are several ways to archive this.
    I would try the following way:
    Every time a user checks a checkbox you add the id to a list of ids. If the user than starts the new activity you serialize your list of ids and transfer it over to the new activity by using an intent. At the new activity you deserialize your list and load every phrase by its id.

    Will this help?

  • PeterDavisPeterDavis ✭✭✭ USMember ✭✭✭

    You can store data in the "Extras" of the intent (Intent.PutExtras) and pass the data that way.

    I prefer to use some sort of data model. You don't necessarily have to have an MVC or MVVM type system, but that's always good. I would probably do something like this if I wasn't doing MVC or MVVM:

    public class MyDataModel
    {
        private MyDataModel() {}
    
        private static MyDataModel _instance;
        public static MyDataModel Instance
        {
            lock(this) // This is here to prevent reentrancy
            {
                if (_instance == null)
                {
                    _instance = new MyDataModel();
                }
            }
            return _instance;
        }
    
        public CheckBoxResults Results { get; set; }
    }
    

    Then in the activity where you've got your results, before you open the new screen, you do something like:

    MyDataModel.Instance.Results = myCheckBoxResults;
    

    And in the new activity you merely need to do:

    var myCheckBoxResults = MyDataModel.Instance.Results;
    
  • DakotaJustinDakotaJustin USMember

    Sebastion is there anyway you can show me an example of this. I am not sure how I would add the ids to a list.

    Also Peter Davis thank you. I will try this and let you know if it works.
    Thank you guys so much.

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

    @DakotaJustin‌ next time you mention someone add a @ in front of his name, so will be notified. Anyways. I put together a small sample that has to be modified to meet you needs. Something like this should show you the way to go.

    [Activity ...]
    public class MainActivity : Activity
    {
        List<int> checkedIds = new List<int>();
    
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
    
            SetContentView(Resource.Layout.Main);
    
            var checkbox = FindViewById<CheckBox>(Resource.Id.checkBox1);
            checkbox.CheckedChange += Checked_Changed; //Every checkbox use this event handler.
    
            FindViewById<Button>(Resource.Id.myButton).Click += delegate
            {
                var intent = new Intent(this, typeof(SecondActivity));
                intent.PutExtra("CheckedIds", checkedIds.ToArray()); //Add the ids of the checked boxes to the intent
    
                StartActivity(intent);
            };
        }
    
        void Checked_Changed (object sender, CompoundButton.CheckedChangeEventArgs e)
        {
            var checkBox = sender as CheckBox; //get the checkbox that fires the event to get the mapping between checkbox and phrase
    
            if (checkBox == null) //if it was not a checkbox that fired the event do nothing
                return;
    
            if (e.IsChecked)
            {
                if (!checkedIds.Contains(checkBox.Id)) // maybe not necessary if no duplicated ids possible
                    checkedIds.Add(checkBox.Id); //you need some identifier to know the mapping between phrase and checkbox.
            }
            else
                checkedIds.Remove(checkBox.Id); //remove id if checkbox got unchecked
        }
    }
    
    [Activity]
    public class SecondActivity : Activity
    {
        List<int> checkedIds;
    
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
    
            SetContentView(Resource.Layout.Main);
    
            checkedIds = Intent.GetIntArrayExtra("CheckedIds").ToList();
    
            if (checkedIds == null)
            {
                Toast.MakeText(this, "No ids provided", ToastLength.Long);
                Finish();
            }
    
            var checkbox = FindViewById<CheckBox>(Resource.Id.checkBox1);
    
            foreach (var checkBoxId in checkedIds)
            {
                //do something to load the phrases or whatever you need to do
            }
        }
    }
    
Sign In or Register to comment.