我正在尝试根据一些条件改变文本视图的颜色。我正在使用Amulya Khare Android set listItem background colour from String Array给出的解决方案。所以现在我已经根据我的要求修改了这段代码。
我的代码如下。
public class MyAdapter extends ArrayAdapter<String> {
Context context;
int layoutResourceId;
String data[] = null;
String color[] = null;
public MyAdapter(Context context, int layoutResourceId, String[] data,
String[] color) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
this.color = color;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
StringHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new StringHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.text1);
row.setTag(holder);
} else {
holder = (StringHolder) row.getTag();
}
holder.txtTitle.setText(data[position]);
// row.setBackgroundColor(Color.parseColor(color[position]));
// holder.txtTitle.setTextColor(Color.parseColor(color[position]));
if (Code.days < 2) {
holder.txtTitle.setTextColor(Color.RED);
}
if (Code.days < 5) {
holder.txtTitle.setTextColor(Color.YELLOW);
}
if (Code.days > 8) {
holder.txtTitle.setTextColor(Color.GREEN);
}
return row;
}
static class StringHolder {
TextView txtTitle;
}
}
我试图做的是,如果天数小于2,则文本颜色应为红色,如果小于5,则文本颜色应为黄色,依此类推。但是我在text-view中只能看到黄色的文本。
另一个问题是,当我从list-view中删除任何数据时,所有数据的颜色都会变为默认颜色,比如黑色。
那么如何解决这个问题呢?提前谢谢。
发布于 2013-12-18 07:40:37
您应该使用如下条件:
if (Code.days < 2) {
holder.txtTitle.setTextColor(Color.RED);
}
else if (Code.days < 5) {
holder.txtTitle.setTextColor(Color.YELLOW);
}
else if (Code.days > 8) {
holder.txtTitle.setTextColor(Color.GREEN);
}
发布于 2013-12-18 07:42:35
尝尝这个
if (Code.days<2)
{
holder.txtTitle.setTextColor(Color.RED);
}
else if (Code.days < 5)
{
holder.txtTitle.setTextColor(Color.YELLOW);
}
else if (Code.days > 8)
{
holder.txtTitle.setTextColor(Color.GREEN);
}
https://stackoverflow.com/questions/20652209
复制相似问题