我在这里看了很多关于如何将答案从一个旋转器发送到另一个活动的答案,但没有一个对我有效。
在我的第一个活动中,我想看看旋转器的位置,这样我就可以相应地更改在第二个活动中出现的问题。下面是我的setupSpinner()方法的代码
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;
}
});
}我把日志消息放在那里,看看哪里出了问题,但是日志消息从来没有出现过。
下面是我的onCreate()方法。我想要保存微调框选择,这样我就可以选择在日志中询问哪些问题。我还想选择" go“来选择"journal”或"exercise“,然后从那里转到正确的活动。我还没有创建一个练习活动,现在只专注于日志。但是,一旦我选择了一个微调选项,我就会被自动发送到日志。
@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);
}
});
}在我的第二个活动中,我想调用一个不同的方法,这取决于所选择的微调器。现在,我的代码只包含微调器位置1的选项,因为我想在添加所有其他位置之前获得正确的逻辑。这是第二个练习。
@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();
}
}非常感谢你的帮助。我花了几个小时试图弄清楚这件事,但我就是不明白出了什么问题。
发布于 2017-02-25 02:24:05
尝试以下操作:添加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)
}使用以下命令获取新活动中的选定值:
Intent intent = getIntent();
int selected = intent.getIntExtra("selected", 0);发布于 2017-02-25 19:56:32
我将我的代码切换到SharedPreferences,这就是在保存它。我仍在努力将其加载到其他活动中,但此选项的效果要好得多。这是我现在的代码。
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;
}
});
}https://stackoverflow.com/questions/42445440
复制相似问题