0

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?

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Eduvm
  • 1,131
  • 8
  • 14

2 Answers2

1

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);
}
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
-1

You are comparing wrongly the Strings, when you do something like this:

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

you are comparing the references and this is not a valid way to verify the equality between objects...

Quick Fix:

replace

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

for

if ("S".equalsIgnoreCase(pedido.getAberto())) {
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97