1

im building an app that has self destructing images and i am trying to pass the value selected from the spinner to another class

final String titles[] = {"1 Second","2 Seconds", "3 Seconds","4 Seconds","5 Seconds","6 Seconds","7 Seconds", "8 seconds, "9 seconds", 10 seconds};
    mSeconds = (Spinner) view.findViewById(R.id.secondsSpinner);
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, titles);
    arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

mSeconds.setAdapter(arrayAdapter);
mSeconds.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
        int secondsToUse = position + 1;
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) {
        // do nothing
    }

});

my intent method:

Intent recipientsIntent = new Intent(getActivity(), RecipientsActivity.class);
 recipientsIntent.putExtra("key1", titles);
                      startActivity(recipientsIntent);

how can i pass the value selected to another class? my method passes the whole array not the item selected.

smithyy
  • 220
  • 2
  • 12

3 Answers3

5
Spinner.getSelectedItem();

Will get you the selected item .

Intent recipientsIntent = new Intent(getActivity(), RecipientsActivity.class);
                recipientsIntent.putExtra("spinnerItem", Spinner.getSelectedItem());
                startActivity(recipientsIntent);

If you need extra logic behind this lmk.. Also Spinner.getSelectedItem() will return an object. You'll need to cast this to whatever you're expecting. A string for example... Spinner.getSelectedItem().toString()

To pass an int in an intent:

Intent recipientsIntent = new Intent(getActivity(), RecipientsActivity.class);
recipientsIntent.putExtra("YOUR_INT_KEY", secondsToUse);
startActivity(recipientsIntent);

The reason your int isn't working in your intent is because it's a local variable to the onItemSelected() Function. make it a member variable or put it outside onItemSelected

cj1098
  • 1,560
  • 6
  • 32
  • 60
  • where do i add `Spinner.getSelectedItem().toString()` in my onItemSelect method – smithyy Aug 19 '15 at 18:22
  • you would put that code outside of your onItemSelect interface method. You would want to put things in your onItemSelect method if you're planning on doing something with the selected value immediately. Or if you're planning on storing the value to be used later. If I knew what you were trying to do with the selected item It'd be easier to guide you lol – cj1098 Aug 19 '15 at 19:13
  • Yes thanks I will be putting it in a timer so I want to take the selected value to another class upload it to parse and then get it back into another class an put it into a timer – smithyy Aug 19 '15 at 19:14
  • so in onItemSelected one of the parameters returns a view I believe. Or a an object value/position. Use that value and pass it via an intent to your other class. If you use the position you could get the value of the object you want from the array you used to populate the Spinner. Then do what you want with it! If you need more help, lmk. Otherwise if my answer was helpful in solving your problem could you mark it as the accepted answer? Thanks! – cj1098 Aug 19 '15 at 19:28
  • please see my edited question above where would i put your answer `Spinner.getSelectedItem().toString()` – smithyy Aug 20 '15 at 09:19
  • Like I said you could use position to get the position of the item selected in your array you used to populate the spinner. Put that code in onItemSelected – cj1098 Aug 20 '15 at 14:22
  • it has red line uner getSelectedItem() – smithyy Aug 20 '15 at 14:36
  • it says not static method getSelectedItem() can not be referenced from static context – smithyy Aug 20 '15 at 15:04
  • don't put getSelectedItem in onItemSelected... that's for outside the interface. in onItemSelected use your position variable to reference the position in your array and use that. – cj1098 Aug 20 '15 at 15:11
  • it wont let me use my variable secondsToUse in the intent, can you please show me how in your answer above – smithyy Aug 20 '15 at 15:13
1
final String titles[] = {"1 Second","2 Seconds", "3 Seconds","4 Seconds","5 Seconds","6 Seconds","7 Seconds", "8 seconds, "9 seconds", 10 seconds};  
Bundle b=new Bundle();
b.putStringArray(key, titles);
Intent i=new Intent(context, Class);
i.putExtras(b);
startActivity(i);

In order to read:

Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray(key);
Noman Rafique
  • 3,735
  • 26
  • 29
1

Here is the code dis shut work

To send:

Intent intentS=new Intent(getActivity(), Main_ExerciseLibrary_Overview_Activity.class);
                    intentS.putExtra("yourInt",Value);
                    startActivity(intentS);

TO Receive:

Bundle b = getIntent().getExtras();
    int myint = b.getInt("yourInt");