Android spinner : perform item selected and and compare it to another spinner

我怕爱的太早我们不能终老 提交于 2020-01-06 00:52:32

问题


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");
    }
}

回答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.




回答2:


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");
    }
}


来源:https://stackoverflow.com/questions/13855857/android-spinner-perform-item-selected-and-and-compare-it-to-another-spinner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!