8 Replies - 9555 Views - Last Post: 17 April 2014 - 06:54 AM Rate Topic: -----

#1 JinG   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 25-September 08

How to get value from spinner

Posted 16 April 2014 - 08:25 PM

Hello,

I am using Visual Studio 2013, C#, and Xamarin studio to develop a mobile app. I have a spinner but cannot get the selected item to display in a textview. It keeps showing 0 for the selected item every time. I am using the setButton_Click for the button event handler. Here is the code:

...
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            Button buttonSet = FindViewById<Button>(Resource.Id.setButton);
            buttonSet.Click += setButton_Click;

            Spinner spinnerHours = FindViewById<Spinner>(Resource.Id.spinnerHours);

            spinnerHours.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(spinnerHours_ItemSelected);
            var adapterHours = ArrayAdapter.CreateFromResource(
                this, Resource.Array.hoursArray, Android.Resource.Layout.SimpleSpinnerItem);

            adapterHours.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
            spinnerHours.Adapter = adapterHours;

            Spinner spinnerMinutes = FindViewById<Spinner>(Resource.Id.spinnerMinutes);

            spinnerMinutes.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(spinnerMinutes_ItemSelected);
            var adapterMinutes = ArrayAdapter.CreateFromResource(
                this, Resource.Array.minutesArray, Android.Resource.Layout.SimpleSpinnerItem);

            adapterMinutes.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
            spinnerMinutes.Adapter = adapterMinutes;

            Spinner spinnerSeconds = FindViewById<Spinner>(Resource.Id.spinnerSeconds);

            spinnerSeconds.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(spinnerSeconds_ItemSelected);
            var adapterSeconds = ArrayAdapter.CreateFromResource(
                this, Resource.Array.secondsArray, Android.Resource.Layout.SimpleSpinnerItem);

            adapterSeconds.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
            spinnerSeconds.Adapter = adapterSeconds;

            timer = new Timer();
            
            int hours = Int32.Parse(spinnerHours.SelectedItem.ToString());
            int convertHours = hours * 1000 * 3600;

            int minutes = Int32.Parse(spinnerMinutes.SelectedItem.ToString());
            int convertMinutes = minutes * 1000 * 60;

            int seconds = Int32.Parse(spinnerSeconds.SelectedItem.ToString());
            int convertSeconds = seconds * 1000;

            int milliseconds = convertHours + convertMinutes + convertSeconds;

            timer.Interval = 1000;

            _counter = milliseconds;

            _countTime = _counter.ToString();
        }

        private void setButton_Click(object sender, EventArgs e)
        {
            timer.Start();
            TextView countdown = FindViewById<TextView>(Resource.Id.countdown);
            countdown.Text = _countTime;
        }
...


This post has been edited by JinG: 16 April 2014 - 08:25 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to get value from spinner

#2 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: How to get value from spinner

Posted 16 April 2014 - 09:40 PM

Where do you get the values from the spinners in the button handling code?

I also don't see where you assign an event handler for your timer.

This post has been edited by Momerath: 16 April 2014 - 09:45 PM

Was This Post Helpful? 0
  • +
  • -

#3 JinG   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 25-September 08

Re: How to get value from spinner

Posted 17 April 2014 - 06:10 AM

How do I get the spinner values from the setButton_Click?
Also, here is the code for the timer tick:

        private void timer_Tick(object sender, EventArgs e)
        {
            _counter--;

            for (double i = _counter; i <= 0; i--)
            {
                TextView countdown = FindViewById<TextView>(Resource.Id.countdown);
                countdown.SetText(_countTime, TextView.BufferType.Normal);
            }

            if (_counter == 0)
             timer.Stop();
        }


Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7053
  • View blog
  • Posts: 23,977
  • Joined: 05-May 12

Re: How to get value from spinner

Posted 17 April 2014 - 06:19 AM

I'm sorry if this side steps your question, but you shouldn't be using your UI controls like variables. Your spinner is part of your view. The current value that you are interested is part of your model. The view's only responsibility is to display and update the model. As it is now, you are trying to make the UI part of your model. This goes against the MVC, MVVM, MVP patterns espoused by modern software.
Was This Post Helpful? 0
  • +
  • -

#5 JinG   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 25-September 08

Re: How to get value from spinner

Posted 17 April 2014 - 06:22 AM

This may sound like a stupid question, but how do I write the code without using the UI as controller. What do I do? I am very new to C#, just started a few weeks ago with no prior knowledge except for some experience with Java.

Also is there a thorough tut showing how to code the parts to respond to the UI? I watched some videos but it seems like I did the same thing they did.

This post has been edited by JinG: 17 April 2014 - 06:23 AM

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7053
  • View blog
  • Posts: 23,977
  • Joined: 05-May 12

Re: How to get value from spinner

Posted 17 April 2014 - 06:24 AM

Anyway, back on topic... It look like your button click handler only pulls the value of _countTime, and based on the all the code you've present so far _countTime is only ever set in your create event handler. Your tick event handler does not update it. You have no code in your button click handler to pull values from your spinner.
Was This Post Helpful? 0
  • +
  • -

#7 JinG   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 25-September 08

Re: How to get value from spinner

Posted 17 April 2014 - 06:33 AM

Do I use

spinnerHours.SelectedItem.toString()


in order to find out what was chosen in the spinnerHours variable?
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7053
  • View blog
  • Posts: 23,977
  • Joined: 05-May 12

Re: How to get value from spinner

Posted 17 April 2014 - 06:50 AM

You mean to tell me you had no idea what you were doing on lines 41-47 of your original post? Did you even write that code or get it from somebody else?
Was This Post Helpful? 0
  • +
  • -

#9 JinG   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 25-September 08

Re: How to get value from spinner

Posted 17 April 2014 - 06:54 AM

To be honest, no. I am using google to learn C# and saw on an online tutorial how to do this. I copied the code according to my UI but have not the slightest how to call the spinner value from setButton_Click. I tried to make a class level variable of spinnerHours then using the same code in 41-46 but it did not work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1