Android Studio - 一个活动中的2个旋转器

时间:2016-04-26 01:50:08

标签: java android android-layout android-studio

首先,我是android和java的新手。刚学习,我需要一些帮助。现在这就是这项活动的工作方式。

有两个微调器。旋转器A - 旋转器B

Spinner A包含“Majors”列表(例如,I.T,Medicine,Vet)

Spinner B包含一个程序列表,它们位于每个“Major”下。所以在I.T下有东西链接MIS或DATABASE等。

现在,Spinner B必须根据Spinner A进行更改。所以如果Spinner A选择了I.T,那么Spinner B应该只显示I.T程序。如果该人将他们对Spinner A的选择更改为Medicine,那么Spinner B必须立即更改并仅显示Medicine程序。

现在我已经完成了所有这些工作。但是,我遇到的问题是我无法使onItemSelectedListerner工作(即,它只是从Spinner A中搜索选择,但是忽略了微调器B的任何选择。)

我通过在所有Spinner B的选择上设置Toast来测试这一点,所以当你在微调器B中选择一些东西时,你应该得到一个祝酒词,但这不会发生。

所以Spinner 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) {

}

}

1 个答案:

答案 0 :(得分:0)

在OnItemSelected中,您需要为第一个微调器编写两个案例,为其他微调器编写其他案例

{{1}}