android - 如何根据基本适配器列表视图中的不同字符串更改颜色
问题描述
如果字符串等于“Sell”,则文本颜色为红色。否则,如果为“Buy”,则文本颜色为绿色。下面是我的代码。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//View view =super.getView(position, convertView, parent);
View view = convertView;
if(view == null){
view = LayoutInflater.from(mContext).inflate(R.layout.list_adv_item,parent,false);
}
Advertisement currAdv = advList.get(position);
TextView date = (TextView)view.findViewById(R.id.textView_date);
date.setText(currAdv.getmDate());
date.setTextColor(Color.WHITE);
TextView name = (TextView) view.findViewById(R.id.textView_amount);
name.setText(currAdv.getmAmount() + " at");
TextView price = (TextView) view.findViewById(R.id.textView_price);
price.setText(currAdv.getmPrice());
TextView type = (TextView) view.findViewById(R.id.textView_type);
if (type.getText() == "Sell") {
type.setTextColor(Color.RED);
} else {
type.setTextColor(Color.parseColor("#00FF00"));
}
type.setText(currAdv.getmType());
return view;
}
}
解决方案
改变:
if (type.getText() == "Sell") {
至:
if (type.getText().equals("Sell")) {
否则你比较引用而不是字符串
推荐阅读
- php - 有没有一种方法可以让 JWT 签名令牌在 larave 中的刀片视图中使用
- php - 使用php在浏览器问题中显示图像
- python - Youtube-DL 搜索视频的返回 URL
- python - Python argparse:如何引用带有破折号的参数
- javascript - ReactJS - 在回调函数中异步打开 Modals 是一种好习惯吗?
- debugging - 无法使用 Comma IDE 进行调试
- python - 如何在导入之前导入需要命令行参数的python脚本?
- tensorflow.js - 数据集生成的特征数据缺少所需的输入键'dense_Dense1_input'
- javascript - 元素不会重定向到另一个页面并且不可点击
- .net - 找不到任何已安装的 .NET Core SDK 问题