0

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.

2
Contributors
3
Replies
6
Views
6 Years
Discussion Span
Last Post by peter_budo
0

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.

Edited by Nickl246

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.