首页 > 解决方案 > 如何根据基本适配器列表视图中的不同字符串更改颜色

问题描述

如果字符串等于“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;
    }

}           

标签: androidstringlistviewcolors

解决方案


改变:

if (type.getText() == "Sell") {

至:

if (type.getText().equals("Sell")) {

否则你比较引用而不是字符串


推荐阅读