gpt4 book ai didi

java - 安卓 ListView : how to change background color of specific cells (by index)?

转载 作者:行者123 更新时间:2023-11-30 03:38:33 25 4
gpt4 key购买 nike

您好,提前致谢

有没有办法改变特定单元格的背景颜色?即按索引号?我试图通过添加 - 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() 是您创建的方法,如果您不将其作为参数传递,我不明白您如何设置特定 View 的颜色。你不想调用 convertView 吗? setBackgroundColor()相反?

关于java - 安卓 ListView : how to change background color of specific cells (by index)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16222288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com