为什么要建立setBackgroundColor不工作在我的自定义的ListView [英] Why set setBackgroundColor is not working in my custom listView

查看:114
本文介绍了为什么要建立setBackgroundColor不工作在我的自定义的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的ListView。主要布局XML是这样的:

I have a custom listView. The main layout xml is something like this:

<?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是这种

The list item XML is this

<?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>

现在的适配器是这样的:

Now the adapter is like this:

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()方法是行不通的。我知道这是在做什么,因为当我用这个方法pssed项目更改的默认颜色不透明的版本$ P $的背景颜色,当一个列表视图项是pressed。

The setBackgroundColor() method is not working. I know that is doing something because when I use this method the background color of the pressed item changes to a opaque version of the default color when a listview item is pressed.

这个问题的背景颜色才会发生,我可以改变一切没有问题...

This problem happen only with the background color, I can change everything else without problems...

谢谢!

推荐答案

使用

setBackgroundResource(R.color.rojo);

R.color.rojo是一种资源,它不是肤色。

R.color.rojo is a resource, it is not color..

这篇关于为什么要建立setBackgroundColor不工作在我的自定义的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆