Uhehesh
Uhehesh

Reputation: 516

ListView setting background color

I have this code in my ArrayAdapter:

public View getView(int position, View convertView, ViewGroup parent) {
    View row = super.getView(position, convertView, parent);

    Toast.makeText(getApplicationContext(), getItem(position).getString("text") + " = " + getItem(position).getString("my"), Toast.LENGTH_SHORT).show();

    if(getItem(position).getString("my") == "0") {
        row.setBackgroundColor(getResources().getColor(R.color.ekvi));
    }
    TextView tv = (TextView) row.findViewById(android.R.id.text1);
    tv.setText(getItem(position).getString("text"));
    tv.setTextColor(Color.BLACK);

    return row;
}

Though background color isn't applied. I checked "my" string and it is really sometimes "0", so it must work.

What am I doing wrong?

Upvotes: 0

Views: 164

Answers (1)

Samir Mangroliya
Samir Mangroliya

Reputation: 40426

Use .equals() to compare Strings objects.

  • == compares Strings Refrences(memory location)

  • .equals() compares Strings characters(value)

Like

if(getItem(position).getString("my").equals("0")) {

Upvotes: 2

Related Questions