Android中为ListView的Item设置不同颜色背景

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = LayoutInflater.from(context).inflate(R.layout.my_bank_card_item, null);
        holder.tv_bank_card = (TextView) convertView.findViewById(R.id.tv_bank_card);
        holder.iv_bank_card = (ImageView) convertView.findViewById(R.id.iv_bank_card);
        holder.tv_num = (TextView) convertView.findViewById(R.id.tv_num);
        holder.tv_savings_card = (TextView) convertView.findViewById(R.id.tv_savings_card);
        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }
    holder.tv_bank_card.setText(datas.get(position).getBankcard());
    holder.tv_num.setText(datas.get(position).getNum());
    holder.tv_savings_card.setText(datas.get(position).getCardtype());
    //设置item的不同背景颜色
    String bankname=datas.get(position).getBankcard();
    if (bankname.equals("中国农业银行")||bankname.equals("中国民生银行")){//绿色
        convertView.setBackgroundColor(Color.parseColor("#0BDEB5"));
    }else if (bankname.equals("中国银行")){//红色
        convertView.setBackgroundColor(Color.parseColor("#FF4C3A"));
    }else if (bankname.equals("中国建设银行")){//蓝色
        convertView.setBackgroundColor(Color.parseColor("#1AB7F3"));
    }else if (bankname.equals("中国光大银行")){//黄色
        convertView.setBackgroundColor(Color.parseColor("#FFAC26"));
    }

    return convertView;
}

猜你喜欢

转载自blog.csdn.net/qq_35572449/article/details/80886686