spinner help

Discussion in 'Android App Developers' started by Woodard2589, Aug 25, 2010.

  1. Woodard2589
    Offline

    Woodard2589 New Member

    Joined:
    Jan 29, 2010
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
    I'm new to most of this and I'm trying to figure out how to have my app react to a spinner selection. I've seen tutorials using setOnItemSelectedListener but i get errors with it.

    Heres my code:

    hellospinner.java
    main.xml
    arrays.xml
    And this is the code i tried using:
    can someone give me the exact code i need? if i don't see the code i can't quite figure it out yet, i'm better at reverse engineering it you could say.
  2. iPirate
    Offline

    iPirate New Member

    Joined:
    Dec 26, 2009
    Messages:
    1,437
    Likes Received:
    0
    Trophy Points:
    0
    what is the error? maybe a line #?
  3. Woodard2589
    Offline

    Woodard2589 New Member

    Joined:
    Jan 29, 2010
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
    I get 3 errors when i add that code. Here they are:

    The method onItemSelected(AdapterView, View, int, long) of type new AdapterView.OnItemSelectedListener(){} must override or implement a supertype method


    The type new AdapterView.OnItemSelectedListener(){} must implement the inherited abstract method AdapterView.OnItemSelectedListener.onItemSelected(AdapterView<?>, View, int, long)


    View cannot be resolved to a type

    I've tried using Quick Fix to solve them, but it can't.
  4. SixStringTheory
    Offline

    SixStringTheory New Member

    Joined:
    Jun 25, 2010
    Messages:
    129
    Likes Received:
    0
    Trophy Points:
    0
    You want to make a new class to handle selection events that implements OnItemSelectedListener like this

    Code:
    public class MyOnItemSelectedListener implements OnItemSelectedListener {
    
        public void onItemSelected(AdapterView<?> parent, view, int pos, long id) {
          // Do something cool!
        }
    
        public void onNothingSelected(AdapterView<?> parent) {
          // Do nothing.
        }
    }
    and then add the listener to your spinner back in onCreate()

    Code:
    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
    That will probably clear up all three errors
    Last edited: Aug 26, 2010
  5. Woodard2589
    Offline

    Woodard2589 New Member

    Joined:
    Jan 29, 2010
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
    ok, this is what i have now:
    and it tells me
  6. SixStringTheory
    Offline

    SixStringTheory New Member

    Joined:
    Jun 25, 2010
    Messages:
    129
    Likes Received:
    0
    Trophy Points:
    0
    Spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

    should be "hubspinner.setOnItem............."

    and I don't know if it's just copy/paste translation adding spaces buy there's a space after the "s" in simple_spinner_dropdown_item. Make sure that's not there in code

    Code:
    adapter.setDropDownViewResource(android.R.layout.s imple_spinner_dropdown_item);
  7. Woodard2589
    Offline

    Woodard2589 New Member

    Joined:
    Jan 29, 2010
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
    Thanks! It works now
  8. SixStringTheory
    Offline

    SixStringTheory New Member

    Joined:
    Jun 25, 2010
    Messages:
    129
    Likes Received:
    0
    Trophy Points:
    0
  9. Woodard2589
    Offline

    Woodard2589 New Member

    Joined:
    Jan 29, 2010
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
    another question. been trying to figure out how to figure out what the user selected. tried

    but it always returns -1. a few of the methods i tried returned long numbers that were different depending on the selection, but i couldnt figure out what to do with it, and cant figure out what i used again. any help?
  10. SixStringTheory
    Offline

    SixStringTheory New Member

    Joined:
    Jun 25, 2010
    Messages:
    129
    Likes Received:
    0
    Trophy Points:
    0
    Use "parent.getItemAtPosition(pos).toString()" in your onItemSelectedListener class. So it should look something like this. "tv" is a new TextView I made.

    Code:
    	public class MyOnItemSelectedListener implements OnItemSelectedListener {
    
    	    public void onItemSelected(AdapterView<?> parent,
    	        View view, int pos, long id) {
    	      tv.setText(parent.getItemAtPosition(pos).toString());
    	    }
    
    	    @SuppressWarnings("unchecked")
    		public void onNothingSelected(AdapterView parent) {
    	      // Do nothing.
    	    }
    	}
  11. Woodard2589
    Offline

    Woodard2589 New Member

    Joined:
    Jan 29, 2010
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
    Big thanks again. took some playing with stuff to get it to work, but it works perfect now. I've been running into a lot of problems with not being able to use variables declared in other classes without putting final in front of it. so what exactly does that do, is it limiting me from how i can use it at the same time? Also I keep getting warning say ArrayAdapter is a raw type, and i have no idea what that means, and just uses @surpresswarning to hide it for now.
  12. SixStringTheory
    Offline

    SixStringTheory New Member

    Joined:
    Jun 25, 2010
    Messages:
    129
    Likes Received:
    0
    Trophy Points:
    0
    ArrayAdapter wants you to tell it what type of data is going into it. Since you're using strings or charsequences you can use that. But it's not an error that'll prevent compiling, just a warning. Weird things CAN happen but in this case wouldn't. You could also use a <?> and then you're just telling it you don't really know before hand.

    the Final keyword's a bit trickier. It's a general Java concept. For variables it means the variable can only be set once. It doesn't make it immutable (ie: once it's set that variable can never ever change). But generally you need to declare varialbes as Final to make them accessible to a scope other than their own parent class.

    Think of it this way. You declare a variable, some int, and that int gets passed to a constructor of some anonymous class. But something else changes that int variable before it gets used in that anonymous class. So now the value that gets used is different from what you sent.

    So if it's declared final it'll get set and sent to the constructor, but since it's final it CAN'T change, and the anonymous class uses the right data.

    So that's why you can use any random "int x" inside its own class, but to make it available outside that class you need to use modifiers to change it's scope.

    Sort of. I'm probably a bit wrong but the general concept is there. Scope in java can get a bit tricky. But luckily Eclipse is good with quick fixes on things like that.
  13. Woodard2589
    Offline

    Woodard2589 New Member

    Joined:
    Jan 29, 2010
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
    ok, that definately helps, i was wondering what was up with the <?> in the code you gave.
Similar Threads
  1. mgarney26
    Replies:
    3
    Views:
    532
  2. JnnNicole
    Replies:
    1
    Views:
    496
  3. kees.5
    Replies:
    2
    Views:
    1,105
  4. ehreeahna29
    Replies:
    7
    Views:
    799
  5. jwdechert
    Replies:
    1
    Views:
    3,617
Loading...
Search tags for this page
問題 spinner cannot be resolved to a type
,
android spinner cannot be resolved to a type
,
cannot make a static reference to the non-static method seto
,
description resource path location type the method onitemsel
,
myonitemselectedlistener cannot be resolved to a type
,
onitemselected(adapterview example
,

spinner cannot be resolved to a type

,
spinnr help to android and registration
,
the type must implement the inherited abstract method adapte
,
the type myspinner must implement the inherited abstract met
,

the type new adapterview.onitemselectedlistener(){} must imp