根据三个微调器中的输入从一个 activity 移动到另一个

Moving from one activity to another based on the input in three spinners

我的 activity 中有三个旋转器。所以我必须根据三个微调器的输入打开特定的 activity;如果我在我的第一个微调器中输入“1”,在我的第二个微调器中输入“1”,在我的第三个微调器中输入 "b",我必须打开受人尊敬的 activity 等等不同的组合。

我尝试过使用 if 语句,但此后每次应用程序都崩溃。

请注意代码中的注释这些是我尝试过但没有得到任何结果的代码。

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
private Button  btn;
/*TextView selection1;
TextView selection2;
TextView selection3;*/
Spinner spin1;
Spinner spin2;
Spinner spin3;

String[] years = {"1","2","3","4"};
String[] branches = {"CSE","ECE","EEE","CIVIL","IT","MECH"};
String[] sections = {"A","B","C"};

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

    Spinner spin1 = (Spinner) findViewById(R.id.spinner1);

    spin1.setOnItemSelectedListener(this);

    /*List<String> branches = new ArrayList<String>();
    branches.add("CSE");
    branches.add("ECE");
    branches.add("IT");
    branches.add("MECH");
    branches.add("CIVIL");
    branches.add("EEE");*/

    ArrayAdapter<String> dataAdapter =new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,years);

    //Drop down layout style - list view with radio button
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    //attaching dataAdapter to spinner
    spin1.setAdapter(dataAdapter);

    Spinner spin2 = (Spinner) findViewById(R.id.spinner2);
    spin2.setOnItemSelectedListener(this);
    ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,branches);
    dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spin2.setAdapter(dataAdapter2);

    Spinner spin3 = (Spinner) findViewById(R.id.spinner3);
    spin3.setOnItemSelectedListener(this);
    ArrayAdapter<String> dataAdapter3 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,sections);
    dataAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spin3.setAdapter(dataAdapter3);

    Button btn= (Button) findViewById(R.id.display);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            displaycheck();
        }
    });
}
public void displaycheck(){
    /*Spinner s1 = (Spinner)findViewById(R.id.spinner1);
    Spinner s2 = (Spinner)findViewById(R.id.spinner2);
    Spinner s3 = (Spinner)findViewById(R.id.spinner3);

    String sp1 = s1.getPrompt().toString();
    String sp2 = s2.getPrompt().toString();
    String sp3 = s3.getPrompt().toString();

    if (sp1.equals("3")&&sp2.equals("CSE")&&sp3.equals("B")){*/
    Toast.makeText(this, "Welcome", Toast.LENGTH_LONG).show();
    Intent it = new Intent(this,M2.class);
    startActivity(it);
    //}
    /*else{
        Toast.makeText(this, "Invalid input", Toast.LENGTH_LONG).show();
    }*/
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    //on selecting a spinner item
    String item = parent.getItemAtPosition(position).toString();
    /*if (spin1.getSelectedItem().toString().equals("3")) {
        if (spin2.getSelectedItem().toString().equals("CSE")) {
            if (spin3.getSelectedItem().toString().equals("B")) {*/
    Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
                /*Intent it = new Intent(this, M2.class);
                startActivity(it);*/
}

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

}

    /*Button btn =(Button)findViewById(R.id.display);
    btn.setOnClickListener(new View.OnClickListener(){
                               @Override
                               public void onClick(View v){
                                   displaycheck();
                               }
                           }
    );
}

public void displaycheck(){
    Spinner s1 =(Spinner) findViewById(R.id.spinner1);
    Spinner s2 = (Spinner) findViewById(R.id.spinner2);
    Spinner s3 = (Spinner) findViewById(R.id.spinner3);
    String ye = y.getText().toString();
    String br = b.getText().toString();
    String se = s.getText().toString();
    if (ye.equals("3")&&br.equals("cse")&&se.equals("b")) {
        Toast.makeText(this, "ra ra ", Toast.LENGTH_LONG).show();
        Intent it = new Intent(this, M2.class);
        startActivity(it);
    }
    else{
        Toast.makeText(this, "po po", Toast.LENGTH_LONG).show();
    }
}*/
}

要从微调器中获取所选项目,请使用此

String value1 = s1.getSelectedItem().toString();
String value2 = s2.getSelectedItem().toString();
String value3 = s3.getSelectedItem().toString();

现在您可以使用任何逻辑检查值,例如:

if(value1.equals("a") && value2.equals("cse") && value3.equals("b"))
   openActivityCodeHere();

您没有为全局变量赋值 - Activity 作用域 - spin1, spin2, spin3.

相反,您正在为 onCreate 或方法特定范围赋予价值;当您从中获取值时,它从未被初始化。

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
private Button  btn;

Spinner spin1;
Spinner spin2;
Spinner spin3;

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

// get spinner in variable
spin1 = (Spinner) findViewById(R.id.spinner1);
...
// Your adapter, ItemListener, DropDownViewResource

spin2 = (Spinner) findViewById(R.id.spinner2);
... // Your adapter, ItemListener, DropDownViewResource 

spin3 = (Spinner) findViewById(R.id.spinner3);
... // Your adapter, ItemListener, DropDownViewResource

Button btn= (Button) findViewById(R.id.display);
btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        displaycheck();
    }
 });
}


public void displaycheck(){
 String sp1 = spin1.getSelectedItem().toString();
 String sp2 = spin2.getSelectedItem().toString();
 String sp3 = spin3.getSelectedItem().toString();

 // Your combinations check
 if (sp1.equals("3")&&sp2.equals("CSE")&&sp3.equals("B")){
 // based on combination - redirect user
  }
 }
}