【问题标题】:how to change color according to different string in base adapter list view如何根据基本适配器列表视图中的不同字符串更改颜色
【发布时间】:2018-11-06 07:45:00
【问题描述】:

如果字符串等于“Sell”,文本颜色将为红色。否则如果 “买”,颜色为绿色。下面是我的代码。

 @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;
    }

}           

【问题讨论】:

  • 那有什么问题?

标签: android string listview colors


【解决方案1】:

变化:

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

到:

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

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

【讨论】:

  • 工作但列表视图映射不正确。请帮助
  • :-) - 移动 type.setText(currAdv.getmType());以上 if (type.getText().equals("Sell")) {
  • 你应该调试这个来发现问题,我只能给你各种提示,最后一个是替换这个 if 为:if (currAdv.getmType().equalsIgnoreCase("Sell")) {
猜你喜欢
  • 1970-01-01
  • 2012-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多