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. Accessing SharedPreferences in Services and non-Activities

> I have some shared preferences (user_id, email) that I want to access

First, do not implement a constructor. Move your code into onCreate() in
your service. Activities, services, etc. should not implement
constructors, or if they do, they should not expect Android API methods to
work from those constructors.

Second, if for some reason you do implement a constructor, please chain to
the superclass.

Third, I would switch to PreferenceManager.getDefaultSharedPreferences(),
particularly if you intend to use the preference API (PreferenceScreen) to
collect the preferences.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com
Android App Developer Books: http://commonsware.com/books.html



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

2. Get active context dynamically of an application

Hi,

How can get the active context of an application?

The scenario is, I have an AsyncTask inside an activity_A. And on successful
completion (means in onPostExecute) of this AsyncTask I want to show a
dialog

(AlertDialog.Builder builder = new AlertDialog.Builder(*Activity_A.this*);)


But in the meanwhile(before completing this AsyncTask) if the application is
moved to another activity_B. The above dialog context will not be the
current Context so it will through an Exception.


So is it possible to get the active Context dianamically? Thanks



--
Best Regards,
Atif Gulzar

I  Unicode, zln 

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

3. How can I get Activity object from Intent object or something else

4. slider keyboard open detection

5. Android as a virtual PBX

6. Camera Button Broadcast Receiver Problem...

7. How to detect All Ram Memory?