java - Gridview背景颜色在Android中滚动时改变颜色
问题描述
GridView
每当我滚动时,项目颜色会随机变化。
每个项目的颜色都基于 SQLite 数据库的状态,但是当我尝试多次滚动时,它会随机更改项目的颜色。有没有办法解决这个问题?这就是它的样子。
这是代码
获取视图
private class ViewHolder{
ImageView imageView, mPreviewCashCard;
TextView txtName, txtPrice, txtSeriesNo;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
Cursor cursor = MainActivity.sqLiteHelper.getData("SELECT id,status FROM DarkMode");
while (cursor.moveToNext()) {
DarkModeStatus = cursor.getString(1);
}
View row = view;
ViewHolder holder = new ViewHolder();
if(row == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layout, null);
holder.txtName = (TextView) row.findViewById(R.id.txtName);
holder.txtPrice = (TextView) row.findViewById(R.id.txtPrice);
holder.txtSeriesNo = (TextView) row.findViewById(R.id.txtSeriesNumber);
holder.imageView = (ImageView) row.findViewById(R.id.imgFood);
holder.mPreviewCashCard = (ImageView) row.findViewById(R.id.imgId);
row.setTag(holder);
}
else {
holder = (ViewHolder) row.getTag();
}
Inventory inventory = inventoryList.get(position);
holder.txtName.setText(inventory.getName());
holder.txtPrice.setText(inventory.getPrice());
holder.txtSeriesNo.setText(inventory.getSeriesNumber());
int status = inventory.getStatus();
if (status==0 && DarkModeStatus.matches("false")){
row.setBackgroundColor(Color.parseColor("#FEF8DD"));
}
else if(status==0 && DarkModeStatus.matches("true")){
row.setBackgroundColor(Color.parseColor("#282828"));
}
byte[] CashCardImage = inventory.getImage();
byte[] idImage = inventory.getIdImage();
if(CashCardImage.length > 1)
{
Bitmap bitmap = BitmapFactory.decodeByteArray(CashCardImage, 0, CashCardImage.length);
holder.imageView.setImageBitmap(bitmap);
}
else{
holder.imageView.setImageResource(R.drawable.ic_image);
}
if(idImage.length > 1)
{
Bitmap bitmap2 = BitmapFactory.decodeByteArray(idImage, 0, idImage.length);
holder.mPreviewCashCard.setImageBitmap(bitmap2);
}
else{
holder.mPreviewCashCard.setImageResource(R.drawable.ic_image);
}
return row;
}
解决方案
尝试在 if-else 块处理背景中添加 else 条件。
if (status==0 && DarkModeStatus.matches("false")){
row.setBackgroundColor(Color.parseColor("#FEF8DD"));
}
else if(status==0 && DarkModeStatus.matches("true")){
row.setBackgroundColor(Color.parseColor("#282828"));
}else {
row.setBackgroundColor(Color.parseColor("#FFFFFF")); // white or whichever default background color you are using
}
据我了解,这可以解决问题。
推荐阅读
- vba - MS Access VBA - 循环拆分函数并输出第一个值(如果不是空)或最后一个值
- angular - 在本地开发模式下使用差异加载时,我是否应该为 IE11 的 Angular 项目添加 polyfill?
- tensorflow - 如何决定卷积层的过滤器?
- java - jfreechart-fx 1.0.1 图表是否可以与 fxgraphics2d 构建和呈现的图像交互,或者只是一个图像?
- javascript - 从 Three.js 中的缓冲区(不是路径)加载 STL
- fiware-orion - 如何将历史数据插入固件(日期正确)?
- performance - Jmeter :- 在并发线程中按顺序运行一组采样器
- python - 如何将时间数据转换为数值?
- angular - 在一个 ts 文件中使用多个加载器服务
- javascript - 使用 javascript(无意)打开移动应用程序(如果已安装)