繁体   English   中英

Textview值必须使用if语句更改itemView背景颜色

[英]Textview value must change itemView background color with if statement

可能是非常简单的解决方案,但我无法弄清楚。 我希望recyclerView的itemView的背景颜色根据在Arraylist中设置的textView的值进行更改。

        public void onBindViewHolder(final MyViewTwoHolder holder, int position) {
    holder.tvBrand.setText((CharSequence) modelTwoArrayList.get(position).getBrand());
    holder.tvImage.setImageResource(Integer.parseInt(String.valueOf(modelTwoArrayList.get(position).getImage())));
    holder.tvUnits.setText(String.valueOf(modelTwoArrayList.get(position).getNumberCases()));
    holder.tvPosisie.setText(String.valueOf(modelTwoArrayList.get(position).getPosisie()));
    if(tvPosisie == 6) {
        holder.itemView.setBackgroundColor(Color.RED);
    }
     else if(tvPosisie == 7) {
        holder.itemView.setBackgroundColor(Color.YELLOW);
    } else if (tvPosisie == 4) {
        holder.itemView.setBackgroundColor(Color.BLUE);
    } else holder.itemView.setBackgroundColor(Color.WHITE);
}

什么都没发生。 该应用程序不会崩溃,但背景颜色保持白色。 谢谢您的时间。

更换

tvPosisie == 6

tvPosisie.getText().equals("6");

希望这可以帮助

您应该将代码的一部分更改为以下内容:

if ("6".equals(holder.tvPosisie.getText().toString())) {
    holder.itemView.setBackgroundColor(Color.RED);
} else if("7".equals(holder.tvPosisie.getText().toString())) {
    holder.itemView.setBackgroundColor(Color.YELLOW);
} else if ("4".equals(holder.tvPosisie.getText().toString())) {
    holder.itemView.setBackgroundColor(Color.BLUE);
} else { 
    holder.itemView.setBackgroundColor(Color.WHITE);
}

更改您的ifs: if (tvPosisie.getText().toString() == "6") ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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