How to get id of array in spinner

by daehoon » Tue, 12 May 2009 10:34:14 GMT


Sponsored Links
 Here is code....

Spinner s = (Spinner) findViewById(R.id.spinner);
       ArrayAdapter adapter = ArrayAdapter.createFromResource(
               this, R.array.planets,
android.R.layout.simple_spinner_item);
       adapter.setDropDownViewResource
(android.R.layout.simple_spinner_dropdown_item);
       s.setAdapter(adapter);

<string-array name="planets">
       <item>first</item>
       <item>second</item>
       <item>third</item>
   </string-array>

I want to use id of array to call other class.....
something like this  swith(i) :
case 0: Intent i = new Intent(this,xxx.class);
           startActivity(i);

case 1: xxxxxxxxxxxxxxxxxxxxxxxxx
how to write exactly. Can anyone help?
Thank you.
--~--~---------~--~----~------------~-------~--~----~



How to get id of array in spinner

by Android Users » Tue, 12 May 2009 12:40:14 GMT


 Add a listener to the spinner..

spinnerObj.setOnItemClickListener(new OnItemClickListener(){

     public void
onItemClick(AdapterView< http://developer.android.com/reference/android/widget/AdapterView.html> ;<?>
parent, View < http://developer.android.com/reference/android/view/View.html> ;
view,
int position, long id){

        // use the position for your case switches

      }
 });

--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


How to get id of array in spinner

by Daehoon Jeon » Tue, 12 May 2009 14:16:14 GMT


 I tried but,, it was not working,,,
Someone on the internet said spinner doesn't support setOnItemClickListener(new
OnItemClickListener()
Is it true ? if it's true how do we set OnItemClickListener?

--~--~---------~--~----~------------~-------~--~----~



How to get id of array in spinner

by Daehoon Jeon » Tue, 12 May 2009 15:08:32 GMT


 It had exception error, something wrong with code, but i can't find out.
Is there anyone help me? thank you
private Spinner s;
        s = (Spinner) findViewById(R.id.dailylog_type);
        ArrayAdapter adapter = ArrayAdapter.createFromResource(
                this, R.array.planets_type,
android.R.layout.simple_spinner_item);

 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s.setAdapter(adapter);
        s.setOnItemSelectedListener(selectListener);

private Spinner.OnItemSelectedListener selectListener = new
OnItemSelectedListener() {
        public void onItemSelected(AdapterView parent, View v, int position,
long id) {
         int pos = s.getSelectedItemPosition();
            switch (pos) {
            case 1:
             Intent i_1 = new Intent(class1.this,class2.class);
             startActivity(i_1);
             break;
            case 2:
             Intent i_2 = new Intent(class1.this,class3.class);
             startActivity(i_2);
             break;
            }
        }
        public void onNothingSelected(AdapterView arg0) {}
    };

--~--~---------~--~----~------------~-------~--~----~



How to get id of array in spinner

by Android Users » Tue, 12 May 2009 15:09:59 GMT


 try setOnItemSelected() method..... worked for me..

--~--~---------~--~----~------------~-------~--~----~



How to get id of array in spinner

by Android Users » Tue, 12 May 2009 15:12:27 GMT


 What is the exception you are getting?
You can directly use the position parameter in the onItemSelected rather
than trying to get the position explicitly.

--~--~---------~--~----~------------~-------~--~----~



How to get id of array in spinner

by Daehoon Jeon » Tue, 12 May 2009 15:18:43 GMT


 would you mind show me an simple example about using setOnItemSelected()
method with position parameter?

--~--~---------~--~----~------------~-------~--~----~



How to get id of array in spinner

by Android Users » Tue, 12 May 2009 16:39:05 GMT


 The code you have posted is fine. just instead of
int pos = s.getSelectedItemPosition();

you can use,

int pos = position;

Think that should work.

May be i confused you with the method name in my previous post.[?] i meant
setOnItemSelectedListener(selectListener) only...

--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Talkshow bahas android iradio

Skr lagi live bahas android xperia x10 di iradio.

Let's tune..

Fey
Sent from my BlackBerry
powered by Sinyal Kuat INDOSAT

-- 
"Indonesian Android Community [id-android]" 

2. Bagaimana cara apps2sd Hero w/ Villain5.2

All,

Mo nanya lagi nih,
Saya punya HTC Hero rooted dengan ROM Villain 5.2,
sepertinya belum apps2sd,
bagaimana cara apps2sd-nya ya ?

suwun,
Agung

-- 
"Indonesian Android Community [id-android]" 

3. ubuntu 10.04 emulator not launching

4. How to create Android library in Eclipse?

5. source code of libcamera, liboemcamera and libaudio

6. Cm507 test2

7. Retrieving a Layout/View programmatically problem