为什么set setBackgroundColor在我的自定义listView中不起作用

时间:2011-11-09 19:08:25

标签: android listview adapter

我有一个自定义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()方法不起作用。我知道这样做是因为当我使用这种方法时,当按下listview项时,按下的项目的背景颜色会变为默认颜色的不透明版本。

这个问题只发生在背景颜色上,我可以毫无问题地改变其他一切......

谢谢!

5 个答案:

答案 0 :(得分:56)

使用

setBackgroundResource(R.color.rojo);

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

答案 1 :(得分:4)

您也可以使用setBackgroundColor(),但您需要了解期望对象不是资源ID。所以你必须将资源转换为颜色对象,如下所示:

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

答案 2 :(得分:0)

要通过setBackgroundColor方法设置颜色,请执行以下操作: -

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

通过

{{1}}

答案 3 :(得分:0)

class CustomerDetailViewset(APIView):
    
     parser_classes = (MultiPartParser, FormParser)

    
     def post(self, request, *args, **kwargs):


        file_serializer = CustomerDetailUploader(data=request.data)
        if file_serializer.is_valid():
            file_serializer.save()
        return Response(file_serializer.data, status=status.HTTP_201_CREATED)
        return Response(file_serializer.errors, status=status.HTTP_400_BAD_REQUEST)

答案 4 :(得分:0)

@RemotableViewMethod
public void setBackgroundColor(@ColorInt int color) {
    if (mBackground instanceof ColorDrawable) {
    ((ColorDrawable) mBackground.mutate()).setColor(color);
    computeOpaqueFlags();
    mBackgroundResource = 0;
    }else {
    setBackground(new ColorDrawable(color));
    }
}

它的声明是这样的。