简体   繁体   中英

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?

Use .equals() to compare Strings objects.

  • == compares Strings Refrences(memory location)

  • .equals() compares Strings characters(value)

Like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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