Need help with spinner

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

Need help with spinner

Postby Mats » Sun Nov 01, 2009 3:59 pm

I'm just started with android developing but I'm facing a problem.

I made a good working spinner but how do i use it? I want that when i select for example "AP_KEU1-1" that i get it in a variable so i can use it.

The spinner java code
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Spinner kiesklas = (Spinner) findViewById(R.id.lijstklas);
  2.  
  3. ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.klassen, android.R.layout.simple_spinner_item);
  4.  
  5. adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  6.  
  7. kiesklas.setAdapter(adapter);
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


The XML file called array.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <resources>
  4.  
  5.     <string-array name="klassen">
  6.  
  7.         <item>-</item>
  8.  
  9.         <item>AP_KEU1-1</item>
  10.  
  11.         <item>AP_KEU1-2</item>
  12.  
  13.         <item>AP_KEU1-3</item>
  14.  
  15.         <item>AP_KEU1-4</item>
  16.  
  17.         <item>AP_KEU1-5</item>
  18.  
  19.         .....
  20.  
  21.     </string-array>
  22.  
  23. </resources>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
Mats
Junior Developer
Junior Developer
 
Posts: 10
Joined: Sun Nov 01, 2009 3:44 pm

Top

Re: Need help with spinner

Postby Mats » Sun Nov 01, 2009 4:29 pm

I now figured out how to get the selected value but my last problem is that the onItemSelected gives an error.

When i place the "public void onItemSelected" outside the onCreate method the errors are gone but the it isn't triggerd.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Spinner kiesklas = (Spinner) findViewById(R.id.lijstklas);
  2.  
  3. ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.klassen, android.R.layout.simple_spinner_item);
  4.  
  5. adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  6.  
  7. kiesklas.setAdapter(adapter);
  8.  
  9.  
  10.  
  11. public void onItemSelected(AdapterView parent, View v, int position, long id) {   // THIS DOESN'T WORK
  12.  
  13.  
  14.  
  15.         String input = parent.getSelectedItem().toString();
  16.  
  17. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
Mats
Junior Developer
Junior Developer
 
Posts: 10
Joined: Sun Nov 01, 2009 3:44 pm

Postby I_Artist » Sun Nov 01, 2009 4:40 pm

Hi Mats,

Welcome to the Android Dev community!

Did you remember to add

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MyClass extends Activity implements AdapterView.OnItemSelectedListener {
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


at the end of your class?
The I_Artist

follow me on Twitter: @I_Artist
follow Android Development on Twitter: #androiddev

Show the world that you are an #androiddev, wear the badge http://www.twibbon.com/join/androiddev
User avatar
I_Artist
Developer
Developer
 
Posts: 33
Joined: Fri Oct 16, 2009 3:49 am
Location: St-Eustache, Quebec

Postby Mats » Sun Nov 01, 2009 5:21 pm

i did that and he auto created these methods below the onCreate method

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         @Override
  2.  
  3.         public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
  4.  
  5.                         long arg3) {
  6.  
  7.                 // TODO Auto-generated method stub
  8.  
  9.                
  10.  
  11.         }
  12.  
  13.  
  14.  
  15.         @Override
  16.  
  17.         public void onNothingSelected(AdapterView<?> arg0) {
  18.  
  19.                 // TODO Auto-generated method stub
  20.  
  21.                
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Now my java is like this but is still doesnt work, it execute but it wont do a thing (the onItemSelect...)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.example.helloandroid;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.os.Bundle;
  8.  
  9. import android.view.View;
  10.  
  11. import android.widget.AdapterView;
  12.  
  13. import android.widget.ArrayAdapter;
  14.  
  15. import android.widget.Spinner;
  16.  
  17. import android.widget.TextView;
  18.  
  19.  
  20.  
  21. public class hrorooster extends Activity implements AdapterView.OnItemSelectedListener {
  22.  
  23.                
  24.  
  25.     /* Called when the activity is first created. */
  26.  
  27.     @Override
  28.  
  29.     public void onCreate(Bundle savedInstanceState) {
  30.  
  31.         super.onCreate(savedInstanceState);
  32.  
  33.         setContentView(R.layout.main);
  34.  
  35.        
  36.  
  37.         Spinner kiesklas = (Spinner) findViewById(R.id.lijstklas);
  38.  
  39.         ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.klassen,
  40.  
  41.         android.R.layout.simple_spinner_item);
  42.  
  43.         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  44.  
  45.         kiesklas.setAdapter(adapter);
  46.  
  47.  
  48.  
  49.     }
  50.  
  51.  
  52.  
  53.         @Override
  54.  
  55.         public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
  56.  
  57.                        
  58.  
  59.                         String input = arg0.getSelectedItem().toString();
  60.  
  61.                        
  62.  
  63.                         TextView weeknr = (TextView) findViewById(R.id.week);
  64.  
  65.                        
  66.  
  67.                         weeknr.setText(input);         
  68.  
  69.         }
  70.  
  71.  
  72.  
  73.         @Override
  74.  
  75.         public void onNothingSelected(AdapterView<?> arg0) {
  76.  
  77.                 // TODO Auto-generated method stub
  78.  
  79.                
  80.  
  81.         }      
  82.  
  83. }  
Parsed in 0.041 seconds, using GeSHi 1.0.8.4
Mats
Junior Developer
Junior Developer
 
Posts: 10
Joined: Sun Nov 01, 2009 3:44 pm

Postby qlimax » Sun Nov 01, 2009 6:22 pm

Hi Mats , and welcome :)

at the (actual)end of the your onCreate method

you have to do:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. kiesklass.setOnItemSelectedListener(this);
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


because you have to tell the spinner what's his listener...

btw,
you can do the same thing in this way

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.   kiesklas.setOnItemSelectedListener(new OnItemSelectedListener() {
  2.  
  3.  
  4.  
  5.                         public void onItemSelected(AdapterView<?> arg0, View arg1,
  6.  
  7.                                         int arg2, long arg3) {
  8.  
  9.  
  10.  
  11.                                         .....
  12.  
  13.                                
  14.  
  15.                         }
  16.  
  17.  
  18.  
  19.                         public void onNothingSelected(AdapterView<?> arg0) {
  20.  
  21.                                
  22.  
  23.                                         .....
  24.  
  25.                                
  26.  
  27.                         }
  28.  
  29.                 });
Parsed in 0.037 seconds, using GeSHi 1.0.8.4



in this way you don't have to implement the interface OnItemSelectedListener at the class level.


Bye :run:
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby Mats » Sun Nov 01, 2009 6:55 pm

Thanks a lot, this works!

:D :D
Mats
Junior Developer
Junior Developer
 
Posts: 10
Joined: Sun Nov 01, 2009 3:44 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 7 guests