How to get the selected item of Android Spinner? - android

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

Related

Update Spinner from another spinner dynamically

I have a fragment that creates controls at runtime, first create a spinner and then the second, the detail is in the second spinner depends on having the first selection.
As I can update the data of the second spinner depending on the selection of the first?
final Spinner Combo2 = new Spinner(FichaRutasVerif2SeccionSlideFragment.this.getActivity());
final Spinner Combo1 = new Spinner(FichaRutasVerif2SeccionSlideFragment.this.getActivity());
List<String> list = new ArrayList<String>();
list.add("TERRESTRE");
list.add("FLUVIAL");
list.add("AEREO");
final ArrayAdapter<String> adapterCombo = new ArrayAdapter<String>(FichaRutasVerif2SeccionSlideFragment.this.getActivity()
,android.R.layout.simple_spinner_item,list);
List<String> list2 = new ArrayList<String>();
list2.add("OP1");
list2.add("OP2");
final ArrayAdapter<String> adapterCombo2 = new ArrayAdapter<String>(FichaRutasVerif2SeccionSlideFragment.this.getActivity()
,android.R.layout.simple_spinner_item, list2);
if(listFormato.get(posicion).cSeccion.trim().equals("M"))
{
adapterCombo.setDropDownViewResource(R.layout.spinner);
Combo1.setAdapter(adapterCombo);
Combo1.setSelection(listFormato.get(posicion).iIndexCombo);
llTempZZ.addView(Combo1);
Combo1.setGravity(Gravity.CENTER);
llTempXX.addView(llTempZZ);
llTEMPADRE.addView(llTempXX);
pllh.addView(llTEMPADRE);
}
else if(listFormato.get(posicion).cSeccion.trim().equals("U")){
final Spinner Combo0 = new Spinner(FichaRutasVerif2SeccionSlideFragment.this.getActivity());
List<String> list3 =new ArrayList<String>();
list3.add("SI");
list3.add("NO");
ArrayAdapter<String> adapterCombo3 = new ArrayAdapter<String>(FichaRutasVerif2SeccionSlideFragment.this.getActivity()
,android.R.layout.simple_spinner_item, list3);
adapterCombo3.setDropDownViewResource(R.layout.spinner);
Combo0.setAdapter(adapterCombo3);
llTempZZ.addView(Combo0);
llTempXX.addView(llTempZZ);
//llTempXX.addView(llTempAA);
llTEMPADRE.addView(llTempXX);
pllh.addView(llTEMPADRE);
Combo0.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView,
View view, int pos, long id) {
if (pos == 0) {
pll2.setVisibility(View.VISIBLE);
}
else if(pos==1) {
pll2.setVisibility(View.GONE);
}
listFormato.get(posicion).vResultado = Combo0.getSelectedItem().toString();
RECORDCARDITEM_DAO.Actualizar(FichaRutasVerif2SeccionSlideFragment.this.getActivity(), listFormato.get(posicion));
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
else if(listFormato.get(posicion).cSeccion.trim().equals("T"))
{
adapterCombo2.setDropDownViewResource(R.layout.spinner);
Combo2.setAdapter(adapterCombo2);
//Combo1.setSelection(listFormato.get(posicion).iIndexCombo);
llTempZZ.addView(Combo2);
Combo2.setGravity(Gravity.CENTER);
llTempXX.addView(llTempZZ);
llTEMPADRE.addView(llTempXX);
//////TEXTOS
Combo2.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView,
View view, int pos, long id) {
if(Combo2.getSelectedItemPosition()==0)
{
}
if (pos ==12) {
llTempAA.setVisibility(View.VISIBLE);
} else {
llTempAA.setVisibility(View.GONE);
}
listFormato.get(posicion).vResultado = Combo2.getSelectedItem().toString();
RECORDCARDITEM_DAO.Actualizar(FichaRutasVerif2SeccionSlideFragment.this.getActivity(), listFormato.get(posicion));
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
You can do something like this
Spinner firstSpinner = (Spinner) findViewById(R.id.first_spinner);
// You can set your first spinner values here if not set already, and then
firstSpinner.setOnItemSelectedListener(onFirstSpinnerChange(firstSpinner));
And the further add this
AdapterView.OnItemSelectedListener onFirstSpinnerChange(Spinner mySpinner) {
return new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// You can add this method to set the values of the second spinner in your activity
// You can use the position, or alternatively selectedItemView for this purpose to identify the selected item
setSecondSpinnerBasedOnFirstOnePositionSelected();
// Or you can just set your spinner in this block
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// do nothing
}
};
}

Android - Get Selected Item from Dynamically created Spinner

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

onclick on a spinner?

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

Adding an onclicklistener to listview (android)

I've managed to implement a great listview that I found here http://www.learn-android.com/2011/11/22/lots-of-lists-custom-adapter/comment-page-1/
but I can't seem to add an onclicklistener
I just want to be able to do an action when I click on the row, with the data that the row contains of course
any ideas?
thanks
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.liste);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Setup the list view
final ListView prestListView = (ListView) findViewById(R.id.list);
final prestationAdapterEco prestationAdapterEco = new prestationAdapterEco(this, R.layout.prestation);
prestListView.setAdapter(prestationAdapterEco);
// Populate the list, through the adapter
for(final prestationEco entry : getPrestations()) {
prestationAdapterEco.add(entry);
}
prestListView.setClickable(true);
prestListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Object o = prestListView.getItemAtPosition(position);
String str=(String)o;//As you are using Default String Adapter
Toast.makeText(getApplicationContext(),str,Toast.LENGTH_SHORT).show();
}
});
}
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object o = prestListView.getItemAtPosition(position);
prestationEco str = (prestationEco)o; //As you are using Default String Adapter
Toast.makeText(getBaseContext(),str.getTitle(),Toast.LENGTH_SHORT).show();
}
});
If your Activity extends ListActivity, you can simply override the OnListItemClick() method like so:
/** {#inheritDoc} */
#Override
protected void onListItemClick(ListView l, View v, int pos, long id) {
super.onListItemClick(l, v, pos, id);
// TODO : Logic
}
The prestListView.getItemAtPosition(position); returns the UI widget: Text, Icon, ...
Try this instead:
Object o = prestationAdapterEco.getItemAtPosition(position);
or
Object o = arg0.getItemAtPosition(position);
Get the object from the adapter. Not from the list-view.
2.
Object o is a prestationEco object. Not a String.
list.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
You are doing
Object o = prestListView.getItemAtPosition(position);
String str=(String)o;//As you are using Default String Adapter
The o that you get back is not a String, but a prestationEco so you get a CCE when doing the (String)o
Try this:
list.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});

Getting spinner information

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

Categories

Resources