Почему setBackgroundColor не работает в моем пользовательском listView

У меня есть собственный listView. Основной макет xml выглядит примерно так:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <ListView android:layout_height="wrap_content" 
              android:id="@+id/lv_clientes"
              android:layout_width="0dp">
    </ListView>
<!-- From this part there are not problems -->
</LinearLayout>

Элемент списка XML - это

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/rlo_elemento"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <TextView android:id="@+id/tv_nombre"
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content">
    </TextView>
    <!-- From this part there are not problems -->
</RelativeLayout>

Теперь адаптер выглядит следующим образом:

public class AdapterListaClientes extends BaseAdapter
{
    private Cliente[] data;
    Context context;
    LayoutInflater layoutInflater;
    int itemSelected = -1;

    public void setSelected(int valor)
    {
        itemSelected = valor;
    }

    public AdapterListaClientes(Context context, ArrayList<Cliente> data)
    {
        this.data = data.toArray(new Cliente[0]);
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }
/*Mandatory things and so...*/

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
            //All the things that we should put in this point.. I'm using the list14 example
        //HERE IS THE POLEMIC CODE
        if(position == itemSelected)
            convertView.setBackgroundColor(R.color.rojo);
        else
            convertView.setBackgroundColor(R.color.blanco);

        return convertView;

}       }

Метод setBackgroundColor() не работает. Я знаю, что это что-то делает, потому что, когда я использую этот метод, цвет фона нажатого элемента изменяется на непрозрачную версию цвета по умолчанию при нажатии элемента списка.

Эта проблема возникает только с цветом фона, я могу без проблем изменить все остальное...

Спасибо!

Ответ 1

Использование

setBackgroundResource(R.color.rojo);

R.color.rojo - ресурс, он не цвет.

Ответ 2

Вы также можете использовать setBackgroundColor(), но вам нужно понять, что ожидает, что объект не является идентификатором ресурса. Таким образом, вам придется преобразовать ресурс в цветной объект, например:

setBackgroundColor(getResources().getColor(R.color.rojo));

Ответ 3

Чтобы установить цвет методом setBackgroundColor, выполните следующие действия: -

setBackgroundColor(Color.parseColor("#e7eecc"));

либо

setBackgroundResource(R.color.<Your-Color>)