как изменить цвет в соответствии с другой строкой в ​​представлении списка базового адаптера - PullRequest
0 голосов
/ 06 ноября 2018

Если строка равна «Продать», цвет текста будет красным. В противном случае, если «Купи», цвет будет зеленый. Вот мой код ниже.

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

}           

1 Ответ

0 голосов
/ 06 ноября 2018

Изменение:

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

до:

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

иначе вы сравниваете ссылки, а не строки

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...