简体   繁体   English

Android-微调器设置TextView可见/不可见

[英]Android - Spinner setting TextView visible/invisible

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" . 我正在尝试做我的第一个Spinner ,但遇到了一些困难,例如,我不知道是否可以通过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语句:

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

TextView code as requested: 要求的TextView代码:

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

是的,您可以做到,并且可以正常工作,但是请使用

buttonSpinner.getSelectedItem().toString().equals("Ultimos 10 lancamentos");

As Stefano has pointed out, your comparison should be using equals (which compares the String contents, vs == which compares the object references). 正如Stefano所指出的,您的比较应该使用equals (比较String的内容,vs ==比较对象的引用)。

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语句应该起作用,但是不清楚从何处调用它(这可能是问题的原因)。 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. 如果要在选择微调项之后立即进行比较,则需要设置一个OnItemSelectedListener并在那里进行比较。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM