2017-02-24 34 views
0

我已經查看了很多關於如何將答案從一個微調器發送到另一個Activity的答案,但是他們都沒有爲我工作。更改第二個Activity中的邏輯取決於第一個Activity中的Spinner

在我的第一個活動中,我想看看微調控制器在哪個位置,這樣我就可以改變我的第二個活動中出現的問題。這裏是我的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(); 
    } 

} 

非常感謝您的幫助。我花了好幾個小時試圖弄清楚這一點,但我不明白什麼是錯的。

+0

「但是這個日誌消息從來沒有出現過」,這很奇怪。你選擇了一個項目嗎? – 0X0nosugar

+0

你的意思是'onItemSelected'方法不會調用微調項目選擇? –

+0

是否讓您的代碼進入'JournalActivity'? – rafsanahmad007

回答

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); 
+0

我試過了,但如果我添加startActivity(意圖),我的代碼會自動進入我的第二個活動(JournalActivity)按鈕或任何東西 – Ang

+0

在設置適配器後添加'grammarChoiceSpinner.setSelection(0,false);'。 – rafsanahmad007

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; 
     } 
    }); 
} 
相關問題