Hy,i need to have an onclick on a spinner.
This is my spinner:
Spinner spinner = (Spinner) findViewById(R.id.spnTarghe);
final String targhe[]=risposta.split("/");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_spinner_item,
targhe
);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
How can I do for use an onclicklistener that do this simple istruction:
sTarga=targhe[position];
By using onitemselected listener you can select item in spinner
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String mselection=spinner.getSelectedItem().toString();
Toast.makeText(getApplicationContext(), "selected "+ mselection, 30).show();
/**** do your code*****/
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
//
}
});
use onItemSelected
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
sTarga=targhe[position];
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
Related
I have dailog with a EditText and a Spinner, i am implementing as below
spinner1 = (Spinner) dialog.findViewById(R.id.quesspinner);
ArrayAdapter<CharSequence> arrayadapter = ArrayAdapter.createFromResource(activity, R.array.fbquestion,R.layout.textview);
spinner1.setAdapter(arrayadapter);
logo.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
spinner1.setVisibility(View.GONE);
spinner1.performClick();
}
});
Now i am unable to read the value of Spinner, i want the text in the Spinner to be displayed in my EditText . code seems to be fine for me but itz not working.Any help is appreciated
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
int Text = parent.getSelectedItemPosition();
edittext.setText(Text);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Use spinner.getSelectedItem() instead of parent.getSelectedItem()
Change your code to like this
To show content in EditText before changing the spinner then do like this
spinner1.setAdapter(arrayadapter);
edittext.setText(spinner1.getSelectedItem().toString());
.
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
String Text = spinner1.getSelectedItem().toString();
edittext.setText(Text);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
I create a Spinner depending on some rules of my app. So, its created dynamically. How can I retrieve which item was selected?
Spinner mySpinner = (Spinner)linearMoreInfo.getChildAt(1);
Using this code to retrieve the created dynamically spinner. But got no idea of how I can handle its click/select item.
Any helps?
I hope it works for you:
mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
Object obj = parent.getItemAtPosition(pos);
...
}
public void onNothingSelected(AdapterView<?> parent) { }
});
Try this Code...
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int pos, long arg3) {
// TODO Auto-generated method stub
selected_item_position = pos;
selected_item = spinner.getSelectedItem().toString();
// You can insert your code...
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
In my application, I use spinner, it shows with spinner.performClick(). After a lot of tries I noticed that my code does not trigger onItemSelected method, Then I tried to add #Override annotation but it returns as error and says delete that annotation. here is that code:
Spinner colorSpinner = new Spinner(this);
ColorFriendsSpinnerAdapter adapter = new ColorFriendsSpinnerAdapter(getApplicationContext(), liste, R.layout.color_dropdown, new String[] { "Icon" }, new int[] { R.id.colorDropdown });
//adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
colorSpinner.setAdapter(adapter);
colorSpinner.performClick();
colorSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {
editColorXml(position);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Thanks for any help, and sorry for my English.
A sample of ItemSelectedListner , no need of override annotation .
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
set listener before colorSpinner.performClick() . in this code spinner will be clicked at the moment where no listener is set .
Try this Code..
final List<String> list=new ArrayList<String>();
list.add("Item 1");
list.add("Item 2");
list.add("Item 3");
list.add("Item 4");
list.add("Item 5");
final Spinner sp1= (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adp1 = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,list);
adp1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adp1);
sp1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), list.get(position), Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
having a little problem getting the information from my spinner, this is how i have it set up in my activity
color = (Spinner)findViewById(R.id.ledColor);
vibrate = (Spinner)findViewById(R.id.vibPattern);
populateSpinners();
color.setOnItemSelectedListener(new colorSelected());
vibrate.setOnItemSelectedListener(new vibrateSelected());
public void populateSpinners(){
ArrayAdapter<CharSequence> cAdapter;
cAdapter = ArrayAdapter.createFromResource(this, R.array.colors,android.R.layout.simple_spinner_item);
int cSpinnerDD = android.R.layout.simple_spinner_dropdown_item;
cAdapter.setDropDownViewResource(cSpinnerDD);
color.setAdapter(cAdapter);
ArrayAdapter<CharSequence> vAdapter;
vAdapter = ArrayAdapter.createFromResource(this, R.array.vibrate, android.R.layout.simple_spinner_item);
int vSpinnerDD = android.R.layout.simple_spinner_dropdown_item;
vAdapter.setDropDownViewResource(vSpinnerDD);
vibrate.setAdapter(vAdapter);
}
public class colorSelected implements OnItemSelectedListener{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
arg0.getItemAtPosition(arg2);
nColor = arg0.toString();
Log.v("EditContact",nColor);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
/********************************************************************************************/
public class vibrateSelected implements OnItemSelectedListener{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
arg0.getItemAtPosition(arg2);
nVibrate = arg0.toString();
Log.v("EditContact",nVibrate);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
but it does not get the selection from the spinner what am i doing wrong?
create a public method like this :
public String returnString(AdapterView<?> parent,int position,long ID){
return parent.getSelectedItem().toString();
}
and put it in your onItemSelected method :
String yourtext = returnString(arg0, arg2, arg3);
Get spinner selected value by using below code..
String value=spinner.getSelectedItem().toString();
How to get a selected item from a spinner in android?
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
}
});
Note:
The documentation for the Spinner widget clearly states:
A spinner does not support item click events. Calling this
method will raise an exception.
Better use OnItemSelectedListener() instead, as above.
In your Activity:
mSpinner = (Spinner) findViewById(R.id.my_spinner);
// Define your adapter, in my case I have a class (SpinnerData) so I get an ID instead of the spinner's text
ArrayAdapter<SpinnerData> myadapter = new ArrayAdapter<SpinnerData>(
this, android.R.layout.simple_spinner_item, new SpinnerData [] {
new SpinnerData("1","Item 1"),
new SpinnerData("2","Item 2")
});
myadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(myadapter);
mSpinner.setOnItemSelectedListener(this);
This is useful if you have other elements in the form that trigger the onItemSelected event:
public void onItemSelected(AdapterView<?> parent, View view, int position, long id{
SpinnerData sd;
switch (parent.getId()) {
case R.id.my_spinner:
sd = (SpinnerData)mSpinner.getSelectedItem();
// do something with sd.id
break;
...
}
As reference, the SpinnerData class:
public class SpinnerData {
public String id;
public String name;
public SpinnerData(String _id, String _name) {
this.id = _id;
this.name = _name;
}
public String toString() {
return this.name;
}
}
Hope it helps.
Try this Code..
sp2.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long id) {
// TODO Auto-generated method stub
int selected_item_position = arg2;
String selected_item=sp2.getSelectedItem().toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});