java如何在CustomAdapter上动态更改单个TextView颜色?
我有一个CustomAdapter,我正试图根据文字类型改变它的颜色
例如
如果字符串“categoria_label”将等于“apple”,我想在文本视图上设置一个红色背景色,如果是“菠萝”,我想设置黄色背景,然后继续。。。这适用于CustomAdapter的每一行
我看过很多教程,我知道在CustomAdapter的getView()函数中可以修改单行
在对象PastChallenge中,有一个getter和一个setter来检索颜色
public String getColore_categoria() {
return colore_categoria;
}
public String setColore_categoria(String colore_categoria) {
this.colore_categoria = colore_categoria;
return colore_categoria;
}
但我不知道如何在CustomAdapter中更改它
也许我需要办个案子
自定义适配器代码
public class PastChallengeAdapter extends ArrayAdapter<PastChallenge> {
ArrayList<PastChallenge> PastChallengeList;
LayoutInflater vi;
int Resource;
ViewHolder holder;
public PastChallengeAdapter(Context context, int resource, ArrayList<PastChallenge> objects) {
super(context, resource, objects);
vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
PastChallengeList = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// convert view = design
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
holder.Categoria = (TextView) v.findViewById(R.id.Categoria);
holder.FirstUserName= (TextView) v.findViewById(R.id.FirstUserName);
holder.secondUserName = (TextView) v.findViewById(R.id.secondUserName);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.Categoria.setText(PastChallengeList.get(position).getCategoria_label());
holder.FirstUserName.setText(PastChallengeList.get(position).getUser_challenge_1());
holder.secondUserName.setText(PastChallengeList.get(position).getUser_challenge_2());
return v;
}
static class ViewHolder {
public TextView Categoria;
public TextView FirstUserName;
public TextView secondUserName;
}
# 1 楼答案
霍尔德。分类。setBackgroundColor((Color.parseColor(PastChallengeList.get)(位置)。getColore_categoria())
# 2 楼答案
尝试将其添加到
getView