0

How do I get spinner selected position in intent and pass it as parameter to another Activity?

 spinner  sp;
if (position==1) {
 do this
} if (position==2) {
 do this
}

Intent i = new Intent(this.getApplicationContext(), AgAppMenu.class);
Bundle bundle = new Bundle();
bundle.putString("mno", mobile.getText().toString());
bundle.putString("pinno", pin.getText().toString());
bundle.putLong("CODE",sp.getSelectedItemPosition());
Iulia Barbu
  • 1,522
  • 12
  • 25
Hayya ANAM
  • 575
  • 2
  • 14
  • 38

4 Answers4

1

Perhaps this is what you want to do?

sp.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> av, View v,
                    int pos, long id) {
                //do something with the pos var
            }
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {}                       
        });
Zesar
  • 566
  • 2
  • 14
0

If I understood correctly. Now all you are missing is

intent.putExtras(bundle);
startActivity(intent);

EDIT:

To get the value, use

(YourType) sp.getSelectedItem();

where YourType is the type of the value you wish to get. If it's Long, cast it to Long.

Zoltán
  • 21,321
  • 14
  • 93
  • 134
  • no no i didnot paste it i want to get spinner selected value at this line bundle.putLong("CODE",sp.getSelectedItemPosition()); – Hayya ANAM Sep 05 '12 at 12:32
  • this line sp.getSelectedItemPosition() did not get spinner selected value – Hayya ANAM Sep 05 '12 at 12:32
  • If you're trying to get the value of the selected item, please edit your question to state that you're looking for the VALUE, not the POSITION. – Zoltán Sep 05 '12 at 12:36
0

define a class variable (here String selectedMonth) and assign it value in the following method and then use this variable in intent

public void onItemSelected(AdapterView month, View arg1,int arg2, long arg3) {

    selectedMonth=  month.getItemAtPosition(arg2).toString();

        }
Rajendra
  • 1,700
  • 14
  • 17
  • i do not want to give hard code value i want to give dynamic value in intent what ever value user select at spiner its go as a parameter to another activity – Hayya ANAM Sep 05 '12 at 12:40
0
String item;
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
item = (String) arg0.getItemAtPosition(arg2);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});

Intent i = new Intent(this.getApplicationContext(), AgAppMenu.class);
Bundle bundle = new Bundle(); bundle.putString("CODE",item);
Harish
  • 3,122
  • 2
  • 31
  • 46