根据第一个Activity中的Spinner更改第二个Activity中的逻辑

时间:2017-02-24 18:04:56

标签: java android

我在这里看了很多关于如何将一个微调器的答案发送到另一个Activity的答案,但是没有一个能为我工作。

在我的第一个活动中,我想看看微调器所处的位置,以便我可以相应地改变我的第二个活动中会出现的问题。这是我的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()方法。我希望保存微调器选择,以便我可以选择在期刊中提出哪些问题。我也想选择“去”选择“日记”或“运动”,然后从那里去正确的活动。我还没有创建一个练习活动,现在只关注日记。但是,只要我选择一个微调器选项,我就会自动发送到期刊。

@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();
    }

}

非常感谢您的帮助。我花了好几个小时试图解决这个问题,但我无法理解错误。

2 个答案:

答案 0 :(得分:1)

尝试以下操作:添加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);

答案 1 :(得分:0)

我将代码切换到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;
        }
    });
}
相关问题