How to use spinners without buttons?

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

How to use spinners without buttons?

Postby mathuin » Tue Jul 21, 2009 9:42 pm

I am writing an application which uses a spinner to select one of five items. I was unable to find an event which is triggered by the selection of an item on a spinner. I was able to work around this lack by adding a button to my UI and triggering my desired activity by the pressing of the button. I don't like "select the item from the spinner and press the button" because it seems clumsy -- how do I get rid of the need for the button and have the simple act of item selection on the spinner trigger an event?

An excellent demonstration of the effect for which I am asking would be to add persistence to the "hello spinner" application. Make a selection, rotate the phone, have the same item be selected, exit the application, re-enter the application, have the same item be selected, that'd be exactly what I want.

Any pointers would be appreciated. I can post my existing code if that helps.
mathuin
Junior Developer
Junior Developer
 
Posts: 15
Joined: Wed Jul 15, 2009 3:10 am

Top

Postby nmc » Wed Jul 22, 2009 12:17 am

I was unable to find an event which is triggered by the selection of an item on a spinner.


Try this :-)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 Spinner s = (Spinner) findViewById(R.id.mySpinner);
  2.                 s.setOnItemSelectedListener(OnItemChanged);
  3.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4



Make a selection, rotate the phone, have the same item be selected, exit the application, re-enter the application, have the same item be selected, that'd be exactly what I want.


Just save the index of the selected item in your preferences, a file or a database.
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Postby mathuin » Wed Jul 22, 2009 1:01 am

nmc wrote:
I was unable to find an event which is triggered by the selection of an item on a spinner.


Try this :-)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 Spinner s = (Spinner) findViewById(R.id.mySpinner);
  2.                 s.setOnItemSelectedListener(OnItemChanged);
  3.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4



This was exactly what I needed. Eclipse was very helpful in populating the stub classes and fixing the argument types, and now I'm two major concepts (handlers and locations) away from completing my first application!

I have since figured out preferences so that's done. Thank you. :-)
mathuin
Junior Developer
Junior Developer
 
Posts: 15
Joined: Wed Jul 15, 2009 3:10 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 7 guests