get which spinner is clicked - android

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

Related

Spinner get position and do different action for each selection

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
}
});

Display another arraylist element in textview using onItemSelected of a spinner

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)
{
}
});

ListView ContextMenu in Fragment

In Fragment onCreateView i have the following code
consumerlist=(ListView)view.findViewById(R.id.consumerlist);
consumerlist.setAdapter(new consumerListAdapter(getActivity(), dataList));
consumerlist.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listview, View view, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "ID"+id, 1);
}
});
Button btn=(Button)view.findViewById(R.id.button1);
registerForContextMenu(consumerlist);
return view;
if i write registerForContextMenu(btn) then it works.but list view row long click does nothing.Even my listView onItemClick listener does not work.how to fix it?Anybody helps me greatly appreciated.Thanks in advance.
try this:
consumerlist.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
registerForContextMenu( view );
openContextMenu( view );
return false;
}
});
also this tutorial should help you a lot ...
//EDIT
Move
registerForContextMenu(consumerlist);
to
public void onActivityCreated(Bundle savedState) {
Remove scollview in layout or define it as not focusable make the code working.

Disable buttons based on spinner choice

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
}});

Android - How to add selection from Spinner to EditText

what I'm trying to do is make a selection from a spinner in android and then whatever is selected to be added to an edittext box. The code I have so far is this...
spinner.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
edittext.setText("");
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
//add some code here
}
);
Problem is this seems to be run even before the spinner is selected so it always sets my edittext to "". Ideally I would like to have it set the text to the selection made in the spinner. So, anyone have any ideas?
At startup, your spinner will get its defaultvalue, that counts as a selection.
Do a boolean FirstTime or something like that.
You probably initialize your spinner from some array or something?
The function actually looks like this
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id);
So just use the position variable
{
edittext.setText(myArray[position]);
}
You can use the getItem method in the adapter to get the object that is shown. Like this:
onItemSelected(AdapterView<?> parent, View view, int position, long id) {
editText.setText((String) adapter.getItem(position));
}

Categories

Resources