Spinner setOnItemSelectedListener

General topics about the Android-Platform itself.
Coding issues please to the subforum right below.

Spinner setOnItemSelectedListener

Postby dragon » Thu Feb 14, 2008 7:40 pm

Hello anyone,

I have read several documentation en threads about this subject but still not working.
I have a spinner with several items.
I want to choose a item but when i run the application he picks only the first item in the spinner and not the item that i choose.

I have a array frees with several items.
here the code i use:
Code: Select all
ArrayAdapter<CharSequence> freestadapter = ArrayAdapter.createFromResource(
                this, R.array.frees, android.R.layout.simple_spinner_item);
                freestSpinner.setAdapter(freestadapter);
                freestSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView parent, View v,
                  int position, long id) {
               freesclick = freestSpinner.getSelectedItem();   //freesclick is a Object i also tried  a String .
               
            }
            
            public void onNothingSelected(AdapterView arg0) {
            
            }

         });


I don't no of the line feesclick = freestSpinner.getSelectedItem(); is correct.
Have anybody experience with this subject.

Thanks in advance.
dragon
Junior Developer
Junior Developer
 
Posts: 16
Joined: Wed Jan 16, 2008 7:36 pm

Top

Postby tum0rc0re » Fri Feb 15, 2008 8:43 am

public void setSelection(int position)

Sets the currently selected item

Parameters

position Index (starting at 0) of the data item to be selected.
JQ Soft web page:
[b][url=http://jqsoft.ru/]http://jqsoft.ru/[/url][/b]
My proffesional blog: [b][url=http://maximyudin.blogspot.com]http://maximyudin.blogspot.com[/url][/b]
Facebook: [b][url=http://www.facebook.com/maxim.yudin]http://www.facebook.com/maxim.yudin[/url][/b]
User avatar
tum0rc0re
Senior Developer
Senior Developer
 
Posts: 158
Joined: Sun Nov 25, 2007 7:15 am
Location: Moscow, Russia

Postby dragon » Fri Feb 15, 2008 5:01 pm

Thanks for your suggestion.

If i set the selection like this:
freestSpinner.setSelection(0); then its take only that item on that position.
The setSelection takes only one int.
That is not what i won't.
I have 7 items in my spinner i want to choose a item and use that item to search into database.

Iám newbee in android.
I have expierience with comboboxen in other language and that was not a problem to activate a item but in android is that for me little more complex.

Thanks in advance.
dragon
Junior Developer
Junior Developer
 
Posts: 16
Joined: Wed Jan 16, 2008 7:36 pm

Postby tum0rc0re » Fri Feb 15, 2008 8:15 pm

I'm sorry, I understand you badly. Say in detail about your task and show total code if it's perhaps.

You have 7 elements, that have names:
1 Element
2 Element
3 Element
4 Element
5 Element
6 Element
7 Element

Do you want programmatically choose e.g. 3 Element? Indeed, freestSpinner.setSelection(2) doesn't fit? Imho, must choose 3 Element. Really not like this?
JQ Soft web page:
[b][url=http://jqsoft.ru/]http://jqsoft.ru/[/url][/b]
My proffesional blog: [b][url=http://maximyudin.blogspot.com]http://maximyudin.blogspot.com[/url][/b]
Facebook: [b][url=http://www.facebook.com/maxim.yudin]http://www.facebook.com/maxim.yudin[/url][/b]
User avatar
tum0rc0re
Senior Developer
Senior Developer
 
Posts: 158
Joined: Sun Nov 25, 2007 7:15 am
Location: Moscow, Russia

Postby dragon » Fri Feb 15, 2008 11:18 pm

first:

I have a dialog thats popup when i push a button in my main layout.
In this this dialog i have a spinner with 7 items ( A array list called frees ).
I have bind this array to the spinner like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ArrayAdapter<CharSequence> freestadapter = ArrayAdapter.createFromResource(
  2.  
  3.                      this, R.array.frees, android.R.layout.simple_spinner_item);
  4.  
  5.              freestSpinner.setAdapter(freestadapter);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


My dialog.xml snippet is like this:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <Spinner id="@+id/spinnerfrees"
  2.  
  3.                         android:layout_height="wrap_content"
  4.  
  5.                         android:layout_width="wrap_content"
  6.  
  7.                         android:drawSelectorOnTop = "true"
  8.  
  9.                         android:paddingBottom="2px"/>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4



Second:

I want to select a item in the spinner like this according the docs.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ArrayAdapter<CharSequence> freestadapter = ArrayAdapter.createFromResource(
  2.  
  3.                      this, R.array.frees, android.R.layout.simple_spinner_item);
  4.  
  5.                      freestSpinner.setAdapter(freestadapter);
  6.  
  7.                      freestSpinner.getSelectedItem();
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

But this doesn't work it shows only the first item in the array(0).
If i select the third item in the array(2) he shows again the first item in the array(0)
That is my problem it doesn't select the item i want but shows only the first item in t.he array(0).

Reading the docs i can use the setOnItemSelectedListener on the spinner so i did that like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ArrayAdapter<CharSequence> freestadapter = ArrayAdapter.createFromResource(
  2.  
  3.                      this, R.array.frees, android.R.layout.simple_spinner_item);
  4.  
  5.              freestSpinner.setAdapter(freestadapter);
  6.  
  7.              freestSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
  8.  
  9.                                 public void onItemSelected(AdapterView parent, View v,
  10.  
  11.                                                 int position, long id) {
  12.  
  13.                                 // What kind code i put here?
  14.  
  15.                                        
  16.  
  17.                                 }
  18.  
  19.                                
  20.  
  21.                                 public void onNothingSelected(AdapterView arg0) {
  22.  
  23.                                
  24.  
  25.                                 }
  26.  
  27.                         });
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

I tried several options in the public void onItemSelectedListener but nothing work.
Maby this help you.

Thanks in advance.
dragon
Junior Developer
Junior Developer
 
Posts: 16
Joined: Wed Jan 16, 2008 7:36 pm

Postby tum0rc0re » Sat Feb 16, 2008 7:53 am

I make SpinnerSample for new SDK - see here, there choose Ukraine (setSelection(2)) - http://www.maximyudin.com/?p=26
JQ Soft web page:
[b][url=http://jqsoft.ru/]http://jqsoft.ru/[/url][/b]
My proffesional blog: [b][url=http://maximyudin.blogspot.com]http://maximyudin.blogspot.com[/url][/b]
Facebook: [b][url=http://www.facebook.com/maxim.yudin]http://www.facebook.com/maxim.yudin[/url][/b]
User avatar
tum0rc0re
Senior Developer
Senior Developer
 
Posts: 158
Joined: Sun Nov 25, 2007 7:15 am
Location: Moscow, Russia

Top

Postby dragon » Tue Feb 19, 2008 6:15 pm

Thanks tumOrcOre and plusminus for suggestions and time.
It works fine now.

I have download the new version SDK (M5).
I don't have any problems more (spinner.setSelectClickListener, Database) all the problems are solved with the new version SDK.
dragon
Junior Developer
Junior Developer
 
Posts: 16
Joined: Wed Jan 16, 2008 7:36 pm

Top

Return to General

Who is online

Users browsing this forum: No registered users and 2 guests