Friday, April 26, 2013

Pass arguments or data from one activity class to another activity class android

Suppose you are in activity class `First` that take some input such as your name and roll number and type of student.
Now you want to show them in another activity class `Second`.

First get name and roll number and type of student from text box and.

EditText txtName = (EditText) findViewById(R.id.txtName);
EditText txtRoll = (EditText) findViewById(R.id.txtRoll);

Spinner m_myDynamicSpinner;
ArrayAdapter<CharSequence> m_adapterForSpinner;
m_myDynamicSpinner = (Spinner) findViewById(R.id.dynamicSpinner);
m_adapterForSpinner = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
m_adapterForSpinner.add("Student");
m_adapterForSpinner.add("Ex-student");
m_adapterForSpinner.add("Employee");
m_myDynamicSpinner.setAdapter(m_adapterForSpinner);

Now call `Second` activity by this way:

Intent intent = new Intent(First.this, Second.class);
intent.putExtra("txtName", txtName.getText().toString());
intent.putExtra("txtRoll", txtRoll.getText().toString());
intent.putExtra("txtStudentType", m_myDynamicSpinner.getSelectedItem().toString());
startActivity(intent);

And get the values from `Second`  activity class by:
getIntent().getExtras().getString("txtName");
getIntent().getExtras().getString("txtRoll");
getIntent().getExtras().getString("txtStudentType");

No comments:

Post a Comment