在我的显示适配器中,我可视化数据库中的数据。我希望列的M值相对于TextView转,呈红色。我怎么能这样做?
public DisplayAdapter(Context c, ArrayList<String> id,ArrayList<String> ore,ArrayList<String> turno) {
this.mContext = c;
this.id = id;
this.turnoName = turno;
this.oreName = ore;
}
public int getCount() {
// TODO Auto-generated method stub
return id.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int pos, View child, ViewGroup parent) {
Holder mHolder;
LayoutInflater layoutInflater;
if (child == null) {
layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.row, null);
mHolder = new Holder();
mHolder.txt_turno = (TextView) child.findViewById(R.id.txt_turno);
mHolder.txt_ore = (TextView) child.findViewById(R.id.txt_ore);
mHolder.txt_id = (TextView) child.findViewById(R.id.txt_id);
child.setTag(mHolder);
} else {
mHolder = (Holder) child.getTag();
}
mHolder.txt_turno.setText(turnoName.get(pos));
mHolder.txt_ore.setText(oreName.get(pos));
return child;
}
public class Holder {
TextView txt_turno;
TextView txt_ore;
TextView txt_id;
}
}
答案 0 :(得分:0)
奇怪......对我有用: 试试这个:
mHolder.txt_turno.setText(turnoName.get(pos));
mHolder.txt_ore.setText(oreName.get(pos));
mHolder.txt_id.setText(dataName.get(pos));
String Turno = turnoName.get(pos);
mHolder.txt_turno.setTextColor(Color.WHITE);
if (Turno != null){
if (TextUtils.equals(Turno,"M"))
mHolder.txt_turno.setTextColor(Color.RED);
if (TextUtils.equals(Turno,"F"))
mHolder.txt_turno.setTextColor(Color.YELLOW);
}
return child;
}
希望能帮到你
答案 1 :(得分:0)
尝试以HTML格式添加文字。我很确定这会改变颜色。您的代码应如下所示:
public View getView(int pos, View child, ViewGroup parent) {
Holder mHolder;
LayoutInflater layoutInflater;
if (child == null) {
layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.row, null);
mHolder = new Holder();
mHolder.txt_turno = (TextView) child.findViewById(R.id.txt_turno);
mHolder.txt_ore = (TextView) child.findViewById(R.id.txt_ore);
mHolder.txt_id = (TextView) child.findViewById(R.id.txt_id);
child.setTag(mHolder);
} else {
mHolder = (Holder) child.getTag();
}
if (turnoName.get(pos).toString().equalsIgnoreCase("M")){
mHolder.txt_turno.setText(Html.fromHtml("<font color='#FF0000'>"+turnoName.get(pos)+"</font>"));
}else if(turnoName.get(pos).toString().equalsIgnoreCase("F")){
mHolder.txt_turno.setText(Html.fromHtml("<font color='#FFFF00'>"+turnoName.get(pos)+"</font>"));
}else{
mHolder.txt_turno.setText(Html.fromHtml("<font color='#FFFFFF'>"+turnoName.get(pos)+"</font>"));
}
mHolder.txt_ore.setText(oreName.get(pos));
return child;
}
使用上面发布的代码更改您的getView。
希望它有所帮助!