Reputation: 83
I've looked through a lot of the answers on here about how to send the answer from a spinner to another Activity, but none of them are working for me.
In my first activity, I want to see which position the spinner is in so I can change questions that will come up in my second activity accordingly. Here is the code for my setupSpinner() method
private void setupSpinner() {
// Create adapter for spinner. The list options are from the String array it will use
// the spinner will use the default layout
ArrayAdapter grammarSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.array_grammar_options,
android.R.layout.simple_spinner_dropdown_item);
// Specify dropdown layout style - simple list view with 1 item per line
grammarSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
//Apply the adapter to the spinner
grammarChoiceSpinner.setAdapter(grammarSpinnerAdapter);
grammarChoiceSpinner.setSelection(0,false);
// Create the intent to send the position to journal activity
grammarChoiceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getApplicationContext(), JournalActivity.class);
intent.putExtra("selected", position);
Log.d("in spinner selected", "position of spinner:" + position );
startActivity(intent);
}
// Because AdapterView is an abstract class, onNothingSelected must be defined
@Override
public void onNothingSelected(AdapterView<?> parent) {
mGrammar = 0;
}
});
}
I put the log message there to see what was going wrong, but that log message never even came up.
Here is my onCreate() method. I want the spinner selection to be saved so I can choose which questions to ask in the journal. I also want to select "go" to choose "journal" or "exercise," then go to the correct activity from there. I have not yet created an exercise activity and am only focusing on journal right now. However, as soon as I select a spinner option, I am automatically sent to the journal.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_opening);
//find the spinner to read user input
grammarChoiceSpinner = (Spinner) findViewById(R.id.spinner);
setupSpinner();
//Set up the intent and OnClickLIstener for the button to go to journal or exercises
final Button chooseGrammarButton = (Button) findViewById(R.id.goButton);
final Button journalButton = (Button) findViewById(R.id.journal);
final Button exercisesButton = (Button) findViewById(R.id.exercises);
// make the Go button disappear and the exercises or grammar button appear
chooseGrammarButton.setVisibility(View.VISIBLE);
journalButton.setVisibility(View.GONE);
exercisesButton.setVisibility(View.GONE);
chooseGrammarButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
chooseGrammarButton.setVisibility(View.INVISIBLE);
journalButton.setVisibility(View.VISIBLE);
exercisesButton.setVisibility(View.VISIBLE);
}
});
//Set button to go to the journal page
Button goToJournal = (Button) findViewById(R.id.journal);
goToJournal.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), JournalActivity.class);
startActivityForResult(myIntent, 0);
}
});
}
In my second activity, I want to call a different method depending on which spinner was selected. Right now, my code only includes the option for spinner position 1 because I wanted to get the logic right before adding all the others. This is the second activity.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_journal);
// nextPresentQuestionButton();
//TODO: fix this so the spinner answer is recorded and the logic follows
Intent intent = getIntent();
int selected = intent.getIntExtra("selected", 0);
Bundle extras = intent.getBundleExtra("selected");
if (extras != null) {
selected = extras.getInt("selected");
}
if (selected == 1) {
nextPresentQuestionButton();
}
}
Thank you so much for the help. I've spent hours trying to figure this out and I just can't understand what's wrong.
Upvotes: 0
Views: 64
Reputation: 83
I switched my code to SharedPreferences and that is saving it. I'm still working on loading it into the other activity, but this option is working much better. Here is my code now.
private void setupSpinner() {
// Create adapter for spinner. The list options are from the String array it will use
// the spinner will use the default layout
final ArrayAdapter grammarSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.array_grammar_options,
android.R.layout.simple_spinner_dropdown_item);
// Specify dropdown layout style - simple list view with 1 item per line
grammarSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
//Apply the adapter to the spinner
grammarChoiceSpinner.setAdapter(grammarSpinnerAdapter);
//Create shared preferences to store the spinner selection
SharedPreferences preferences = getApplicationContext().getSharedPreferences
("Selection", MODE_PRIVATE);
editor = preferences.edit();
// Create the intent to save the position
grammarChoiceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//receive the string of the option and store it
int grammarOptionPosition = grammarChoiceSpinner.getSelectedItemPosition();
//put the string in the editor
editor.putInt("grammarOption", grammarOptionPosition);
editor.commit();
//make a toast so the user knows if it's not "select"
if (grammarOptionPosition != 0) {
Toast.makeText(getApplicationContext(), "Choice saved.",
Toast.LENGTH_SHORT).show();
}
}
// Because AdapterView is an abstract class, onNothingSelected must be defined
@Override
public void onNothingSelected(AdapterView<?> parent) {
mGrammar = 0;
}
});
}
Upvotes: 0
Reputation: 23881
try the following: add startActivity()
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, JournalActivity.class);
intent.putExtra("selected", position);
Log.d("in spinner selected", "position of spinner:" + position );
startActivity(intent)
}
Get the Selected value in new activity using:
Intent intent = getIntent();
int selected = intent.getIntExtra("selected", 0);
Upvotes: 1