您好,谢谢!

有没有办法更改特定单元格的背景颜色?即按索引号?我试图通过添加-if(position == 3)修改下面的教程代码。不知道我在做什么错或者是否允许这样做。任何帮助是极大的赞赏。

public ItemListBaseAdapter(Context context, ArrayList < ItemDetails > results) {
    itemDetailsrrayList = results;
    l_Inflater = LayoutInflater.from(context);
}

public int getCount() {
    return itemDetailsrrayList.size();
}

public Object getItem(int position) {
    return itemDetailsrrayList.get(position);
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = l_Inflater.inflate(R.layout.item_details_view, null);
        holder = new ViewHolder();
        holder.txt_itemName = (TextView) convertView.findViewById(R.id.name);
        holder.txt_itemDescription = (TextView) convertView.findViewById(R.id.itemDescription);
        //  holder.txt_itemPrice = (TextView) convertView.findViewById(R.id.price);
        holder.itemImage = (ImageView) convertView.findViewById(R.id.photo);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    ////////////////////////////  TEST CODE - Header cells ////////////////


      if(position == 2){
      convertView.setBackgroundColor(android.R.color.background_light);
  }

    if (position == 3) {
         convertView.setBackgroundColor(android.R.color.background_light);
    }
    if (position == 14) {
         convertView.setBackgroundColor(android.R.color.background_light);
    }
    /////////////////////////////////////


    holder.txt_itemName.setText(itemDetailsrrayList.get(position).getName());
    holder.txt_itemDescription.setText(itemDetailsrrayList.get(position).getItemDescription());
    // holder.txt_itemPrice.setText(itemDetailsrrayList.get(position).getPrice());
    holder.itemImage.setImageResource(imgid[itemDetailsrrayList.get(position).getImageNumber() - 1]);

    return convertView;
}

最佳答案

据我所知,BaseAdapater中没有setBackground()(如果这是您继承的内容)。如果setBackground()是由您创建的方法,那么如果您不将其作为参数传递,我将无法理解如何设置特定视图的颜色。您不是要调用convertView。setBackgroundColor()吗?

09-03 18:43