Hi, below is my code for an android app I am developing, I was wondering how I can pass the value of pos from the array adapter to another activity through intent?
What I would like is for the code in the next activity to be performed based on the value of the array's position from this file. I understand I have to use putExtras, but I don't understand how exactly to pass this value with the return type of onItemSelected being void.

public class App2Activity extends Activity {

    Button button1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);

        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
        this, R.array.arrTime_Available, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        addListenerButton();

        spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());      

    }

      public void addListenerButton() {
            final Context context1 = this;

            Button button1 = (Button) findViewById(R.id.buttonback);
            button1.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    Intent myIntent = new Intent(context1, Timer.class);

                    startActivity(myIntent);
                }

            });
      }

    public class MyOnItemSelectedListener implements OnItemSelectedListener {



        public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
          Toast.makeText(parent.getContext(), "The time is " +
              parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();

        }

        public void onNothingSelected(AdapterView<?> parent) {
          // Do nothing.
        }

        public int posArray(AdapterView<?> parent, View view, int pos, long id)
        {
            return pos;

        }

    }

}

Thanks for reading.

Recommended Answers

All 3 Replies

SharedPreferences is what you looking for. Before leaving activity A save data in preferences and then in activity B read it out

thank you so much! <3

I added the following to the onItemSelected

  SharedPreferences app_preferences =
                  PreferenceManager.getDefaultSharedPreferences(App2Activity.this);
          SharedPreferences.Editor editor = app_preferences.edit();
          String text = parent.getItemAtPosition(pos).toString();
          editor.putString("key", text);
          editor.commit();
           int index = parent.getLastVisiblePosition(); 
         Toast.makeText(parent.getContext(), "The time is " +
                 index, Toast.LENGTH_LONG).show();

and this in the receiver activity/class

                SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
                String text = app_preferences.getString("key", "null");
                textView.setText( "\n" + text + "\n pomodoro set!");

and it works perfectly! thanks again.

Exactly, that is the whole magic ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.