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.