Search code examples
androidlistviewcustom-adaptercustom-view

View.setVisibility(View.VISIBLE) with custom adapter


I have something like this as my item_row for a listview

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal">

    <View
        android:layout_width="10dp"
        android:layout_height="10dp"
        android:background="@drawable/status_1"
        android:id="@+id/status1"
        android:visibility="gone"/>

    <View
        android:layout_width="10dp"
        android:layout_height="10dp"
        android:background="@drawable/status_2"
        android:id="@+id/status2"
        android:visibility="gone"/>

    <View
        android:layout_width="10dp"
        android:layout_height="10dp"
        android:background="@drawable/status_3"
        android:id="@+id/status3"
        android:visibility="gone"/>

</LinearLayout>

Then, i have this in my adapter.

@Override
    public View getView(int position, View convertView, ViewGroup parent) {

    View v;

    if (convertView == null) {

        v = parent.inflate(context, R.layout.itens_pedidos, null);

    }

    else {

        v = convertView;

    }

    Pedido pedido = (Pedido) getItem(position);

    // Define widgets
    View status1 = (View) v.findViewById(R.id.status1);
    View status2 = (View) v.findViewById(R.id.status2);
    View status3 = (View) v.findViewById(R.id.status3);
    ...

    ...

Now, i am trying to make status# visible if met some condition

    if (pedido.getAberto() == "S") {

        status1.setVisibility(View.VISIBLE);

    }

    if (pedido.getCancelado() == "S") {

        status2.setVisibility(View.VISIBLE);

    }

    if (pedido.getEnviado() == "S") {

        status3.setVisibility(View.VISIBLE);

    }

However, all the lines are getting equal, even if the object does not meets the condition

Any tips?


Solution

  • You shouldn't use == to compare strings in Java. Use the equals() method on a non-null string instead:

    if ("S".equals(pedido.getAberto())) {
        status1.setVisibility(View.VISIBLE);
    }