spinner help

Woodard2589

New Member
Joined
Jan 29, 2010
Messages
28
Reaction score
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
package com.shanewoodard.hellospinner;

import android.app.Activity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

public class HelloSpinner extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Spinner hubSpinner = (Spinner) findViewById(R.id.myspinner);

ArrayAdapter adapter = ArrayAdapter.createFromResource( this, R.array.colors , android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

hubSpinner.setAdapter(adapter);


}


}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:eek:rientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Spinner
android:id="@+id/myspinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
/>

</LinearLayout>
arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="colors">
<item>Red</item>
<item>Blue</item>
<item>White</item>
<item>Yellow</item>
<item>Black</item>
<item>Green</item>
<item>Purple</item>
<item>Orange</item>
<item>Grey</item>
</string-array>
</resources>
And this is the code i tried using:
Spinner s = (Spinner) findViewById(R.id.myspinner);
s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView adapter, View v, int i, long lng) {
//do something here
}

@Override
public void onNothingSelected(AdapterView arg0) {
//do something else
}
});
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.
 
OP
W

Woodard2589

New Member
Joined
Jan 29, 2010
Messages
28
Reaction score
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.
 
Joined
Jun 25, 2010
Messages
129
Reaction score
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:
OP
W

Woodard2589

New Member
Joined
Jan 29, 2010
Messages
28
Reaction score
0
ok, this is what i have now:
package com.shanewoodard.hellospinner;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

public class HelloSpinner extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Spinner hubSpinner = (Spinner) findViewById(R.id.myspinner);

ArrayAdapter adapter = ArrayAdapter.createFromResource( this, R.array.colors ,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

Spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());


hubSpinner.setAdapter(adapter);
}

public class MyOnItemSelectedListener implements OnItemSelectedListener {

public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) {
// Do something cool!
}

public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
}


}
and it tells me
Cannot make a static reference to the non-static method setOnItemSelectedListener(AdapterView.OnItemSelectedListener) from the type AdapterView<SpinnerAdapter>
 
Joined
Jun 25, 2010
Messages
129
Reaction score
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);
 
OP
W

Woodard2589

New Member
Joined
Jan 29, 2010
Messages
28
Reaction score
0
another question. been trying to figure out how to figure out what the user selected. tried

adapter.getPosition(hubSpinner)

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?
 
Joined
Jun 25, 2010
Messages
129
Reaction score
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.
	    }
	}
 
OP
W

Woodard2589

New Member
Joined
Jan 29, 2010
Messages
28
Reaction score
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.
 
Joined
Jun 25, 2010
Messages
129
Reaction score
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.

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.
 
OP
W

Woodard2589

New Member
Joined
Jan 29, 2010
Messages
28
Reaction score
0
ok, that definately helps, i was wondering what was up with the <?> in the code you gave.
 
Top