0

I am using multiple ListViews to create multiple options for the End-user and now I have to use a button to direct the user to his/her desired page. This is my code -

    spinner = (Spinner) findViewById(R.id.spinner);
    spinner1 = (Spinner) findViewById(R.id.spinner1);
    String[] List_view1 = new String[]{
            "Choose your option...",
            "All",
            "1",
            "2"
    };

    String[] List_View2 = new String[]{
            "Choose your option...",
            "All",
            "4",
            "5",
            "6"
    };

    final List<String> list= new ArrayList<>(Arrays.asList(List_view1));
    final List<String> list1= new ArrayList<>(Arrays.asList(List_View2));

    button.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
       switch (v.getId()) {
          case R.id.button:
               String spi = spinner.getSelectedItem().toString();
               String spi1 = spinner1.getSelectedItem().toString();
               if (spi == "Choose your option..." || spi1 == "Choose your option...")
               {
               Toast.makeText(MainActivity.this, "Please make sure you have given every input, " += " and not leaving even one empty", Toast.LENGTH_SHORT).show();
               }
               if (spi == "1" || spi == "2" || spi1 == "5" || spi1 == "6")
               {
               Intent intent = new Intent(getApplicationContext(), CollegeListAES1.class);
               startActivity(intent);
               }
               if (spi == "2" || spi == "3" || spi1 == "4")
               {
               Intent intent = new Intent(getApplicationContext(), CollegeListAES2.class);
               startActivity(intent);
               }   
               break;
            }
        }
    });
}

Now if I select 2 and 4, then it will go to CollegeListAES2, but it is going to CollegeListAES1. surprisingly if I make another if statement like this, the same problem occurs. Talking about first of statement, which is "Choose your Option..." is perfectly working. How to get rid of this and go to the proper page?

2 Answers 2

1

It's because that your every if statement contains only OR operator. As you said when you select 2 and 4 ( spi = 2 , spi1 = 4 ) so your first if statement is true because of your if is :

if (spi == "1" || spi == "2" || spi1 == "5" || spi1 == "6")

So the second one returns true and other values will not check anymore --> spi = 2 then it starts CollegeListAES1 activity.

You need to combine your if statements with OR and AND operators.

Sign up to request clarification or add additional context in comments.

3 Comments

can you tell me about AND operator? how to apply?
And operator : &&
Yeah definitely, Thanks a lot bro, you made my app.
0

Try this one,
Use AlertDialog to create a list of items and put them into singlechoiceItems list.
Whenever you select anyone and click on ok/positive button then you can redirect it anywhere you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.