Search code examples
javaandroidblackjackandroid-toast

Two spinners change text in TextView


I'm trying to make this android app and I have some problems. I want to pick a card number in my left spinner, and in the right spinner is my dealers hand, and I pick a card number there aswell.(see Image below)

My app

I can change my cards and the image updates no problem. But when I pick ex 17 and 2, how do I change the text in my Textview? I have a TextView, but cleared the text. I tried this, but it doesn't work

if(plHand.equals("8") && dlHand.equals("2") || dlHand.equals("3")){
        Toast.makeText(this, "IT WORKS!", Toast.LENGTH_LONG).show();
    }

I made a Toast just to check if something happens. But nothing happens. But how do I do it? plHand and dlHand is a String Array. I also tried this

if(sp1.getSelectedItem().toString().equals("17") && (sp2).getSelectedItem().toString().equals("Ace"){
        Toast.makeText(MainActivity.this, "IT WORKS!", Toast.LENGTH_LONG).show();

    }

Hope you guys know what to do!

EDIT: added more code

final String[] plHand = getResources().getStringArray(R.array.yourHand_array);
final String[] dlHand = getResources().getStringArray(R.array.dealerHand_array);

final Spinner sp1 = (Spinner) findViewById(R.id.spinner1);
final Spinner sp2 = (Spinner) findViewById(R.id.spinner2);

The text I want to change is in a String Array aswell, but I don't have to use the String Array, if there is an easier way!

EDIT!!: It works now! Full code is here! http://pastebin.com/g3M2wbtL


Solution

  • Just attach the same OnItemSelectedListener to both spinners like the following:

    AdapterView.OnItemSelectedListener listener = new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String plHandText = sp1.getSelectedItem().toString();
            String dlHandText = sp2.getSelectedItem().toString();
            if ("17".equals(plHandText) && "2".equals(dlHandText)){
                Toast.makeText(MyActivity.this, "It works!", Toast.LENGTH_LONG).show();
            }
        }
    
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
    
        }
    }
    sp1.setOnItemSelectedListener(listener);
    sp2.setOnItemSelectedListener(listener);