I tried various methods fro internet to pass spinner selected item to other class and display in text view
Below is my code. Whenever I open my app,It crashes. Your suggestions are greatly appreciated. Thanks in Advance.
This is my first class BuddyActivity
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonSubmit = (Button) findViewById(R.id.buttonGo);
buttonSubmit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Spinner transportSpinner = (Spinner) findViewById(R.id.spinnerSection);
Intent i = new Intent(BuddyActivity.this.getApplicationContext(),search_project.class);
i.putExtra("transportSpinnerValue", transportSpinner.getSelectedItem().toString());
BuddyActivity.this.startActivity(i);
}
});
}
;
This is my second class:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_project);
Bundle extras = getIntent().getExtras();
String transportItemChosen = extras.getString("transportSpinnerValue");
}
Class.1.java
Class2.java
Step 1: Create Layout File for Spinner
Step 2: Create a res/values/email.xml. This file will hold the list of values for drop down.
Step 3: Create Spinner object in program, map it to layout file. Create respective adapters. I have a fragment in my main activity. You have to customize the below code, in case you only use Activity.
Step 4: Create Second Activity to receive the data and display it to TextView
I have tested this code and works fine!
Use this peice of code in your BuddyActivity:
And use this peice of code in your second class:
make sure you have register your second activity in your Menifest file.