首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据第一个活动中的微调器更改第二个活动中的逻辑

根据第一个活动中的微调器更改第二个活动中的逻辑
EN

Stack Overflow用户
提问于 2017-02-25 02:04:56
回答 2查看 51关注 0票数 0

我在这里看了很多关于如何将答案从一个旋转器发送到另一个活动的答案,但没有一个对我有效。

在我的第一个活动中,我想看看旋转器的位置,这样我就可以相应地更改在第二个活动中出现的问题。下面是我的setupSpinner()方法的代码

代码语言:javascript
复制
    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“,然后从那里转到正确的活动。我还没有创建一个练习活动,现在只专注于日志。但是,一旦我选择了一个微调选项,我就会被自动发送到日志。

代码语言:javascript
复制
@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的选项,因为我想在添加所有其他位置之前获得正确的逻辑。这是第二个练习。

代码语言:javascript
复制
        @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();
    }

}

非常感谢你的帮助。我花了几个小时试图弄清楚这件事,但我就是不明白出了什么问题。

EN

回答 2

Stack Overflow用户

发布于 2017-02-25 02:24:05

尝试以下操作:添加startActivity()

代码语言:javascript
复制
@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)
        }

使用以下命令获取新活动中的选定值:

代码语言:javascript
复制
Intent intent = getIntent();
int selected  = intent.getIntExtra("selected", 0);
票数 1
EN

Stack Overflow用户

发布于 2017-02-25 19:56:32

我将我的代码切换到SharedPreferences,这就是在保存它。我仍在努力将其加载到其他活动中,但此选项的效果要好得多。这是我现在的代码。

代码语言:javascript
复制
  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;
        }
    });
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42445440

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档