I am trying to use a spinner, save the position of each selection and depend on this, another spinner with different selections for each position appears..
I made the first spinner with the help of the spinner docs but then I can't do something different for each selection.. Is there anyone who can help me?
Thank you!
You can use a switch statement in the onItemSelected() method:
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
switch (pos) {
case 0:
//do action for first selection
break;
case 1:
//do action for second selection
break;
...
}
}
it is too simple to impliment:
try spinner ItemSelectedListener.
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
if(position==0){}//do the function you want to perform
else if(position==1){}//And so on
// your code here
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
Related
I have a spinner named spinnerArray
hot can I get the selected item's ID of spinner by integer data type after click a button?
(when I have selected First one of spinner, I want to get integer 0,Second one,integer 1)
Use
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(final AdapterView<?> parent, final View view, final int position, final long id) {
// position is what you want i think
}
#Override
public void onNothingSelected(final AdapterView<?> parent) {
}
});
And to get the position when clicked on other views use spinner.getSelectedItemPosition();
You can do this in your button's onClick method:
mySpinner.getSelectedItemPosition() + 1;
getSelectedItemPosition() gives you the item position starting at 0, so you need to add 1.
i think you are looking for position in spinner
code sample
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView,
int position, long id) {
// your code here
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
Are you looking to see when the items on the spinner have been clicked? Then OnItemSelectedListener will allow you to override onItemClick(AdapterView parent, View view, int position, long id).
I have 2 arraylists, 1 is use to display the elements to the spinner and another one is use to display on a textview when one of the element from spinner is selected.
Example:
0---a---football
1---b---badminton
2---c---basketball
"a,b,c" are the elements in arraylist1; "football, badminton, basketball" are the elements in arraylist2; "0,1,2" are the index for both arraylists.The index of elements on both of the arraylists has already arranged properly as shown above.
What I want to do now is to let the spinner to display "a,b,c". When I select "b" in the spinner, the textview will show me "badminton".
What should I write in the onItemSelected of the spinner?Any idea for this?
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView,
int position, long id) {
switch(position){
case 0:
textView1.setText(sportsList.get(0));
break;
case 1:
textView2.setText(sportsList.get(1));
break;
case 2:
textView3.setText(sportsList.get(2));
break;
}
}
});
You can get the selected item in the spinner by this code
String selected = spinner1.getText().toString();
Then check for the condition,
if(selected.equals("a")){
textview1.setText(array2.get(0));
}else if(selected.equals("b")){
......
try this,
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3)
{
Textview your_tv = (Textview)findviewbyid(R.id.tv);
your_tv.setText(your_array[position]);
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
I took multiple spinners in single view. I know how to get when item is selected.
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id)
{
parent.getItemAtPosition(pos);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
But how can i know that which spinner user has clicked on in a single listener?
Any suggestion will be appreciated.
Thanks In advance.
Try the following way.
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
switch(parent.getId()){
case R.id.spinner1:
//your code goes here
break;
case R.id.spinner2:
//your code goes here
break;
}
}
Refer here and here too.
parent in onItemClick refers to the Spinner that was clicked.
AdapterView.OnItemSelected
parent The AdapterView where the selection happened
Spinner is an AdapterView. Spinner
I am working on my first ever android app, and I'm creating basic exercise/calorie counter. I have two spinners, one for the selected type of exercise, and one for the time spent preforming said exercise in minutes. I need to be able to check the value/position of both spinners so I can do something like this:
PSUEDO CODE:
if(Exercise spinner = "push-ups")
{
CaloriesBurned = TimeSpinnerValue*450
}
if(Exercise spinner = "sit-up")
{
CaloriesBurned = TimeSpinnerValue*350
}
etc . . . nothing fancy. My spinners are populated from a String Array in my String.xml. But I dont know how get the value of the spinner so I can use it in some IF statements in my java code.
use like this for compair any string with your spinner.
spinner.equals("push-ups");
You need to implement OnItemSelectedListener for getting the selected value from the Spinner. Then override,
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
int id = parent.getId();
switch (id) {
case R.id.first_spinner:
// your stuff here
break;
case R.id.second_spinner:
// your stuff here
break;
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
yourSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View arg1, int position, long arg3) {
String selectedString = (String) (yourSpinner.getAdapter()).getItem(position);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}});
check this link fro more info
Generically, you'll want to follow this pattern:
SpinnerAdapter adapter = Spinner.getAdapter();
int position = Spinner.getSelectedItemPosition();
Object value = adapter.getItem(position);
Since you are loading it with String values, you can then cast value to a String.
Have some field in your activity to hold value of spinner, initialize with some defaultvalue
implement onItemSelectedListener on spinners, and in onItemSelected get value of selected item, by position argument of onItemSelected(AdapterView adapterView, View view, int position, long id)
So I have buttons (not the soft key pad) on the screen but I want some to be disabled (can't click) when you pick a certain spinner option. Like I have the buttons 0-9 (for numeric input) and if "Base 2" (spinner selection 0) is picked I want all the buttons except 0 and 1 to be disabled.
You can add a OnItemClickListener and react to the option that is given like for example this way
spinner.setOnItemClickListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
if(position == 1)
button.setClickable(false);
}
});
Spinner does not support setOnItemClickListener. If you try and use it, you will get an exception:
java.lang.RuntimeException: setOnItemClickListener cannot be used with a spinner
As such, you should use setOnItemSelectedListener:
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
Toast.makeText(MyActivity.this, "position= "+position+" / id= "+id, Toast.LENGTH_LONG).show();
switch(position) {
case 0:
button0.setClickable(true);
button1.setClickable(false);
break;
case 1:
button0.setClickable(false);
button1.setClickable(true);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}});