Key & Value in a spinner

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

Key & Value in a spinner

Postby alexdemers » Sat Aug 08, 2009 10:21 pm

Hey, I was wondering how to put a string array in a spinner (I can, using the ArrayAdapter) but I also want to set keys for these also. But it seems that there's no constructors for that (I may be mistaken).

I have 2 arrays: {"5123", "7434", "7333", "7345" } and {"City 1", "City 2", "City 3", "City 4" }. Obviously, the spinner should show the array of the name of the cities, but in the backend (what's returned from the submition of the form is the value.

Anyone knows how?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. // Put the array in the spinner
  5.  
  6. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, cityNames);
  7.  
  8. spinCities.setAdapter(adapter);
  9.  
  10.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4



While, I'm the subject of spinners: is there any resource on the internet describing every type of spinner layouts? I'm using simple_spinner_item but I find it too small. Any suggestions?

Thanks.
alexdemers
Freshman
Freshman
 
Posts: 5
Joined: Tue Aug 04, 2009 11:14 pm

Top

Postby nmc » Sun Aug 09, 2009 1:09 am

Obviously, the spinner should show the array of the name of the cities, but in the backend (what's returned from the submition of the form is the value.


The simplest way would be using Spinner.getSelectedItemPosition() as index to your numbers-array.

is there any resource on the internet describing every type of spinner layouts? I'm using simple_spinner_item but I find it too small. Any suggestions?


I have not needed this yet but i am sure you can do your own layout just like you would with listviews.
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Postby alexdemers » Sun Aug 09, 2009 1:18 am

nmc wrote:The simplest way would be using Spinner.getSelectedItemPosition() as index to your numbers-array.


Well, the thing is the index can change anytime so I really need a ID of the item. I am fetching JSON from a website and it's provided with the ID and the name.
alexdemers
Freshman
Freshman
 
Posts: 5
Joined: Tue Aug 04, 2009 11:14 pm

Postby nmc » Sun Aug 09, 2009 11:30 am

Well, the thing is the index can change anytime so I really need a ID of the item. I am fetching JSON


...and while doing that, you have something like
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. ids.add(id);
  3.  
  4. names.add(name);
  5.  
  6.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

so the index of a name alway equals the index of its id!?

Anyway, the "java way" would be to create your own adapter and overwrite getItemId() to get the ID stored with a name at a given position.
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 4 guests