在ViewHolder的第二栏中更改文本

时间:2018-08-01 00:18:55

标签: android android-recyclerview recycler-adapter android-viewholder

我想更改第二列的文本颜色,所有小于4.0的文本变为红色。

我的viewHolder代码:

@Override
public void onBindViewHolder(@NonNull ViewHolderDatos holder, int position) {

    holder.punto.setText(puntoNotaList.get(position).getPunto());
    holder.nota.setText(puntoNotaList.get(position).getNota());

    }

2 个答案:

答案 0 :(得分:0)

您可以在if语句中使用setTextColor(),当该值小于4.0时,可以设置Color.RED,否则设置Color.BLUE

在此示例中,我们更改了nota。

示例

...
if (Double.parseDouble(puntoNotaList.get(position).getNota()) < 4) {
    holder.nota.setTextColor(Color.RED);
} else {
    holder.nota.setTextColor(Color.BLUE);
}
...

答案 1 :(得分:0)

AdapterDatos.java

public class AdapterDatos extends RecyclerView.Adapter<AdapterDatos.ViewHolderDatos>{

Context context;
ArrayList<PuntoNota> puntoNotaList;

public AdapterDatos(Context context, ArrayList<PuntoNota> puntoNotaList) {
    this.context = context;
    this.puntoNotaList = puntoNotaList;
}


@NonNull
@Override
public ViewHolderDatos onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list,parent,false);
    ViewHolderDatos viewHolder = new ViewHolderDatos(itemView);
    return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull ViewHolderDatos holder, int position) {

    holder.punto.setText(puntoNotaList.get(position).getPunto());
    holder.nota.setText(puntoNotaList.get(position).getNota());


    if((position % 2) == 0)
        holder.itemView.setBackgroundColor(0xFFeceff1);

    else
        holder.itemView.setBackgroundColor(0xFFffffff);


    }

@Override
public int getItemCount() {
    return puntoNotaList.size();
}


public class ViewHolderDatos extends RecyclerView.ViewHolder {

    TextView punto, nota;

    public ViewHolderDatos(View item) {
        super(item);
        punto = item.findViewById(R.id.idPunto);
        nota = item.findViewById(R.id.idNota);
    }
}

}