简体   繁体   中英

android listview: how to change background color of specific cells (by index)?

Hello and thanks in advance,

Is there a way to changed the background color of specific cells ? Namely by index number ? I am attempting to modify this tutorial code below by adding an - if(position==3). Not sure what I'm doing wrong or if it's even allowed. Any help is greatly appreciated.

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;
}

As far as I can tell there's not setBackground() in BaseAdapater (if that's what you inherit from). If setBackground() is a method created by you, I don't understand how you're able to set the color of a specific view if you don't pass it as an argument. Don't you want to call convertView. setBackgroundColor() instead?

For any other beginners out there adding this snippet to getView() is what worked for me. Hope it helps someone.

 if(position == 0 || position == 7 || position == 9 || position == 15 ){


      convertView.setBackgroundColor(Color.DKGRAY);
  }
  else
  {
      convertView.setBackgroundColor(Color.BLACK);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM