0

I'm trying to do my first Spinner, and I have encountered some difficulties, such as that I don't know if I can get an option by spinner.getSelectItem == "some string".

Take a look at my code so far

Populating the spinner:

public void addItemsOnSpinner() {
    Spinner buttonSpinner = (Spinner) findViewById(R.id.buttonSpinner);
    List<String> list = new ArrayList<String>();
    list.add("Ultimos 5 lancamentos");
    list.add("Ultimos 7 lancamentos");
    list.add("Ultimos 10 lancamentos");
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    buttonSpinner.setAdapter(dataAdapter);
}

Trying to make an if statement:

if(buttonSpinner.getSelectedItem().toString() == "Ultimos 10 lancamentos"){
    textView.setVisibility(View.VISIBLE);
}

TextView code as requested:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Deposito"
    android:visibility="invisible"
    android:id="@+id/textView"
    android:layout_row="2"
    android:layout_column="0"
    android:layout_gravity="center|left" />                

And its code on the class:

TextView textView = (TextView)findViewById(R.id.textView);
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
Cássio Bruzasco
  • 183
  • 2
  • 13
  • What is this textView ? Is it a separate view or you want to hide the spinner's default textView ? – Abhishek May 24 '15 at 18:27
  • Its a separate view. The intention here is to make a list, but Im testing with a textView just to learn the spinner, if I select the third option (ultimos 10 lancamentos) it would show 5 more textView – Cássio Bruzasco May 24 '15 at 18:29
  • 2
    Yes you can do that. Once you extract the text of the selected spinner item successfully, you can do your logic accordingly. And of course, you can set the visibility of the textViews. But in your logic, you can not compare two String values using '=='. You have to use .equals() method for that. – Abhishek May 24 '15 at 18:35

2 Answers2

2

Yes you can do it and it will work fine, but please use

buttonSpinner.getSelectedItem().toString().equals("Ultimos 10 lancamentos");
Stefano Vuerich
  • 996
  • 1
  • 7
  • 28
  • Its still not triggerring the setVisible. I select this option, but the textView remains invisible – Cássio Bruzasco May 24 '15 at 18:31
  • give us the complete code with textView declaration as well please – Stefano Vuerich May 24 '15 at 18:35
  • Instead of using "invisible" try using "gone" property and let us know the effect. – Abhishek May 24 '15 at 18:41
  • Still not working, Im guessing Its missing a trigger or something, because I have add a log on my if statement `Log.d("textDeposito","state:"+textDeposito01.getVisibility());` and Its now showing its log when I select my third option. – Cássio Bruzasco May 24 '15 at 18:46
  • so getVisibility() return INVISIBLE even if you set it to VISIBLE ? – Stefano Vuerich May 24 '15 at 18:56
  • No, the Log isnt even showing, meaning that is not entering on the ifstatement. Maybe because after I select any option of my spinner I have to confirm or something, all codes I read about spinner they make a button to confirm the selected option. Or am I wrong about this? – Cássio Bruzasco May 24 '15 at 18:59
  • did u set setOnItemSelectedListener() ? – Stefano Vuerich May 24 '15 at 19:02
  • 1
    take a look here http://www.mkyong.com/android/android-spinner-drop-down-list-example/ – Stefano Vuerich May 24 '15 at 19:12
  • I was reading this post, and I came across with another problem. He makes a new class for the actions, and my action must be at this separated class. How I can get a button ID or textView ID on a different class than its layout? – Cássio Bruzasco May 24 '15 at 19:17
  • do you mean CustomOnItemSelectedListener ? – Stefano Vuerich May 24 '15 at 19:18
  • Yes, you can notice that the Toast message is at CustomOnItemSelectedListener, so by that meaning my ifstatement must be on that class as well, right? – Cássio Bruzasco May 24 '15 at 19:20
  • you can implement OnItemSelectedListener in you Activity/Fragment or you can create a CustomListener class that implements OnItemSelectedListener. What is really importan are methods onItemSelected() and onNothingSelected() . Can you post your code for those functions? – Stefano Vuerich May 24 '15 at 19:25
  • @CássioBruzasco to add to what Stefano just said, you can declare your OnItemSelectedListener as an anonymous inner class (http://stackoverflow.com/questions/355167/how-are-anonymous-inner-classes-used-in-java) to avoid having to break the code up in inconvenient ways. In other words, declaring it as a separate class (as the example) is just a matter of style. – trooper May 24 '15 at 19:26
  • the code its really close to this tutorial you pasted here. Im trying to make this work, and I will tell you if it did. – Cássio Bruzasco May 24 '15 at 19:30
  • @trooper Im trying to figure how to make this CustomOnItemSeletedListener declaration inside my activity. Thanks for the help so far – Cássio Bruzasco May 24 '15 at 19:35
1

As Stefano has pointed out, your comparison should be using equals (which compares the String contents, vs == which compares the object references).

Otherwise your if statement should work, however its not clear where you are calling it from (and that might be the cause of the problem). If you want to make the comparison immediately after a spinner item is selected then you need to set an OnItemSelectedListener and make the comparison there.

Here is an example of how you might declare this listener inline:

buttonSpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()
{
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
    {
        String selectedItem = parent.getSelectedItem().toString();

        if (selectedItem.equals("Ultimos 10 lancamentos"))
        {
            textView.setVisibility(View.VISIBLE);
        }
    }

    public void onNothingSelected(AdapterView<?> parent)
    {
    }
});
trooper
  • 4,444
  • 5
  • 32
  • 32