2016-04-26 33 views
0

首先,我是android和java的新手。只是學習,我需要一點幫助。現在這就是這項活動應該如何運作。Android Studio - 2個活動中的紡紗機

有兩個紡紗廠。微調A - 微調乙

微調A包含 「專業」(例如,I.T,醫學,獸醫)

微調B包含程序,這些程序在每個 「主」 的名單列表。所以在I.T下面有東西鏈接MIS或DATABASE等。

現在,微調器B必須根據微調器A來改變。所以如果微調器A有I.T選擇,那麼微調器B應該只顯示I.T程序。如果此人將其在Spinner A上的選擇更改爲Medicine,那麼Spinner B必須立即更改並僅顯示Medicine項目。

現在我已經完成了所有這些工作。但是,我遇到的問題是我無法讓onItemSelectedListerner正常工作(即,它僅偵聽來自微調器A的選擇,但忽略來自微調器B的任何選擇)。

我通過在所有Spinner B的選擇上設置Toast來測試這個,所以當你在Spinner B中選擇一些東西時,你應該得到一個敬酒,但是這不會發生。

因此,微調A,聽衆工作正常,但它不聽微調B.幫助請嗎?

此外,我需要Spinner B的「響應」或「動作」的原因是,基於Spinner B的選擇,它將在以下Activity中顯示不同的數據。 (這將是一個列表視圖)

package com.example.chadedwards.sgu; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemSelectedListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Spinner; 
import android.widget.TextView; 
import android.widget.Toast; 


public class ActivityTwo extends Activity implements OnItemSelectedListener { 

String str_student_id, str_semester, str_year, str_studentName, str_studentAddress, str_studentMajor, str_studentProgram; 
EditText student_name, student_address; 
Button int2_submit; 
Spinner student_major,student_program, it_programs, hss_programs, bms_programs, bec_programs; 


@Override 
protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.layout_two); 

    //Retrieve passed data from main activity in BundleA 
    final Bundle bundleA = this.getIntent().getExtras(); 
    str_student_id = bundleA.getString("studentid"); 
    str_year = bundleA.getString("year"); 
    str_semester = bundleA.getString("semester"); 
    //Set the textview at the top of the layout to the ID retrieved from main_activity 
    ((TextView)findViewById(R.id.textView_studentID)).setText("Student ID: " + str_student_id); 


    student_name = (EditText) findViewById(R.id.editText_name); 
    student_address = (EditText) findViewById(R.id.editText_address); 
    int2_submit = (Button) findViewById(R.id.int2_submit); 


    student_major = (Spinner) findViewById(R.id.spinner_major); 
    ArrayAdapter majorAdapter = ArrayAdapter.createFromResource(this, R.array.major_names, android.R.layout.simple_spinner_item); 
    student_major.setAdapter(majorAdapter); 

    student_program = (Spinner) findViewById(R.id.spinner_program); 

    student_major.setOnItemSelectedListener((OnItemSelectedListener) this); 
    student_program.setOnItemSelectedListener((OnItemSelectedListener) this); 




    // Action Performed When Submit Button Is Clicked 
    int2_submit.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      str_studentName = student_name.getText().toString(); 
      str_studentAddress = student_address.getText().toString(); 
      //str_studentMajor = student_major.getText().toString(); 
      //str_studentProgram = student_program.getText().toString(); 

      //Create new intent along with bundle data to pass 
      Intent myIntent2 = new Intent(ActivityTwo.this, ActivityThree.class); 
      Bundle bundleB = new Bundle(); 
      //add data to bundle 
      /* bundleB.putString("studentid", str_student_id); 
      bundleB.putString("semester", str_semester); 
      bundleB.putString("year", str_year); 
      bundleB.putString("studentname", str_studentName); */ 
      bundleB.putString("studentaddress", str_studentAddress); 
      // bundleB.putString("studentmajor",str_studentMajor); 
      bundleB.putString("studentprogram", str_studentProgram); 
      //pass bundle to intent and start the activity using the intent 
      myIntent2.putExtras(bundleB); 
      myIntent2.putExtras(bundleA); 
      ActivityTwo.this.startActivity(myIntent2); 

     } 
    }); 



} 

@Override 
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 

    switch(parent.getId()) { 
     //Run Code For Major Spinner 
     case R.id.spinner_major: 
      switch(position){ 
       case 0: 

        if(parent.getId() == R.id.spinner_program){ 
         Toast.makeText(this, "Yay", Toast.LENGTH_LONG); 
        } 
        ArrayAdapter hssProgramAdapter = ArrayAdapter.createFromResource(this, R.array.hss_programs, android.R.layout.simple_spinner_item); 
        student_program.setAdapter(hssProgramAdapter); 

        Toast.makeText(this, "Position: " + position, Toast.LENGTH_LONG); 


        switch(parent.getId()){ 

         case R.id.spinner_program: 

          Toast.makeText(this, "TESTING", Toast.LENGTH_LONG); 
          break; 

        } 



        break; 

       case 1: 

        ArrayAdapter itProgramAdapter = ArrayAdapter.createFromResource(this, R.array.it_programs, android.R.layout.simple_spinner_item); 
        student_program.setAdapter(itProgramAdapter); 

        Toast.makeText(this, "Position: " + position, Toast.LENGTH_LONG); 

        break; 

       case 2: 

        ArrayAdapter bmsProgramAdapter = ArrayAdapter.createFromResource(this, R.array.bms_programs, android.R.layout.simple_spinner_item); 
        student_program.setAdapter(bmsProgramAdapter); 

        Toast.makeText(this, "Position: " + position, Toast.LENGTH_LONG); 

        break; 

       case 3: 

        ArrayAdapter becProgramAdapter = ArrayAdapter.createFromResource(this, R.array.bec_programs, android.R.layout.simple_spinner_item); 
        student_program.setAdapter(becProgramAdapter); 


        Toast.makeText(this, "Position: " + position, Toast.LENGTH_LONG); 

        break; 

      }//switch positions 
      break; 
     }//switch parents 


} 

@Override 
public void onNothingSelected(AdapterView<?> parent) { 

} 

} 

回答

0

在OnItemSelected你需要寫兩個情況之一爲第一微調等其他微調

@Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
{ switch(parent.getId()) 
{ //Run Code For Major Spinner 
case R.id.spinner_major: 
{ // code for first spinner. Depending on spinner.getselecteditem assign adapter to second spinner 
} 
case R.id.second_spinner: 
{ // code for second spinner 
//Use get item selected and get selected item position 
    } 
+0

哎,對不起,這仍然無法正常工作:( – Razoe