1

I'm working with 2 spinner in my app, and I want to do this operation in my button click for example like this in visual studio

if ((spin1.text = "bla bla bla") && (spin2. text = "ho ho ho"))
    {
       text1.text = result;
    }

Do you have any idea to perform this operation in android? thanks.

SOLVED WITH THIS CODE!!

spin1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {
        @Override
        public void onItemSelected(AdapterView<?> parent, View seletedItem, int pos, long id)
        {
            Object item = parent.getItemAtPosition(pos);
            value1 = item.toString();
        }

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

        }
    });

    spin2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {
        @Override
        public void onItemSelected(AdapterView<?> parent, View selectedItem, int pos, long id)
        {
            Object item = parent.getItemAtPosition(pos);
            value2 = item.toString();
        }

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

        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

public void count(View v)
{
    if(value1.equals("Depok") && value2.equals("Jakarta"))
    {
        hasil.setText("SUCCESS");
    }
}
Raditya Kurnianto
  • 734
  • 1
  • 16
  • 42

2 Answers2

1

First you have to retrieve the text from the spinner correctly, did you do that? If not, this is how to do it:

Assuming you called your spinner spinner.

Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
String txtFromSpinner = mySpinner.getSelectedItem().toString();

When comparing, take the string from the spinner and compare it with some string.

if (txtFromSpinner.equals("bla bla bla") && txtFromSpinner2.equals("ho ho ho")
{
   Log.E("Well done sir.","Comparison complete!");
   //Code
}

Good luck.

Dion Segijn
  • 2,625
  • 4
  • 24
  • 41
  • When using eclipse you can open your logcat and see that line appearing. Ofcourse you replace Log.E with your own lines of code, but logcat can be used for fast debugging. Log = logging. E = Error. (Log means outputting in logCat and the E makes the line red.) – Dion Segijn Dec 13 '12 at 08:47
  • I've try your suggest but it did nothing here are my code https://www.evernote.com/shard/s201/sh/a633b54b-64f2-4f35-b4ef-017c0b1d4560/f40c3cdd80b0ecc78998b920e5e04021 – Raditya Kurnianto Dec 13 '12 at 09:19
  • do: Log.E(value1,value2); (Before comparing, and after assigning value1 and value2.) What does it say? what does it try to compare? is it the correct string? edit: Be aware it is case sensitive. – Dion Segijn Dec 13 '12 at 09:27
  • 1
    I think i know what the problem is. mySpinner.getSelectedItem().toString() will not return the string that is selected. probably some other value like memory adress or something else? (did not try it). What you can do instead is: http://stackoverflow.com/a/9524432/1164919 – Dion Segijn Dec 13 '12 at 09:34
  • problem solved by myslef!! Thanks to @Daniel Martinus for giving me the idea to solved it. :D this is the right code https://www.evernote.com/shard/s201/sh/a633b54b-64f2-4f35-b4ef-017c0b1d4560/f40c3cdd80b0ecc78998b920e5e04021 – Raditya Kurnianto Dec 13 '12 at 13:10
0

Try this code once.

String item1,item2;
Textview hasil;

hasil = (TextView) findViewById(R.id.textView1);


spin1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {

    public void onItemSelected(AdapterView<?> parent, View seletedItem, int pos, long id)
       {
          String item1 = (String) parent.getItemAtPosition(pos);
       }

        public void onNothingSelected(AdapterView<?> parent)
        {

        }
});

spin2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{

        public void onItemSelected(AdapterView<?> parent, View selectedItem, int pos, long id)
        {
           String item2 = (String) parent.getItemAtPosition(pos);

        }

        public void onNothingSelected(AdapterView<?> parent)
        {

        }
    });
}

public void count(View v)
{
    if(item1.equals("Depok") && item2.equals("Jakarta"))
    {
        hasil.setText("berhasil");
    }
}
Dion Segijn
  • 2,625
  • 4
  • 24
  • 41
User
  • 1,251
  • 1
  • 14
  • 19