嘿,大家我不是一个专业的编码员,但我一直在玩一个简单的待办事项列表来学习android开发的基础知识。ListView颜色问题
我已经能够获得所有我想要工作的东西,但是我的listview有一个问题让我完全陷入困境。我扩展了SimpleCursorAdapter,以便格式化来自我的sqlite数据库的数据,并根据duedate是否过期来更改duedate文本的颜色。格式工作完美无瑕,但我得到了一些奇怪的结果与颜色。
listview中的前几个条目看起来像我期望的那样,但是当我向下滚动时,不一致会开始弹出,其中没有截止日期设置的项目将以红色或绿色着色。我在列表中上下滚动的次数越多,出现的不一致性就越大,直到最终每行都被着色,无论它是否应该是。
有人能帮我理解这里发生了什么吗?请看下面我的自定义适配器。
public class ProAdapter2 extends SimpleCursorAdapter {
private static int[] TO = {R.id.priority, R.id.projectname, R.id.duedate};
private static String[] FROM = {"priorities", "projectName", "dueDate"};
private Context context;
private int layout;
//constructor
public ProAdapter2(Context context, int layout, Cursor c) {
super(context,layout, c, FROM, TO);
this.context=context;
this.layout = layout;
}
@Override
public View newView(Context context, Cursor curso, ViewGroup parent){
Cursor c = getCursor();
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(layout, parent, false);
TextView txtName = (TextView) v.findViewById(R.id.projectname);
txtName.setText(c.getString(1));
TextView txtPriority = (TextView) v.findViewById(R.id.priority);
txtPriority.setText(c.getString(2));
return v;
}
@Override
public void bindView(View v, Context context, Cursor c) {
TextView txtDueDate = (TextView) v.findViewById(R.id.duedate);
TextView txtDueDateLabel = (TextView) v.findViewById(R.id.duedate_label);
TextView txtPriority = (TextView) v.findViewById(R.id.priority);
TextView txtPriorityLabel = (TextView) v.findViewById(R.id.priority_label);
TextView txtName = (TextView) v.findViewById(R.id.projectname);
LinearLayout pridate = (LinearLayout) v.findViewById(R.id.pridate);
String dueDate = c.getString(3);
String cDate = c.getString(4);
String dueDateFormated;
MyTime t = new MyTime();
Long cTimeLong = c.getLong(6);
Long dTimeLong = c.getLong(5);
dueDateFormated = t.getFormatedTime(c.getString(3));
txtDueDate.setText(dueDateFormated);
if (c.getInt(5)==0){
txtDueDate.setText("Not Set");
}
else if (cTimeLong < dTimeLong){
txtDueDate.setTextColor(Color.GREEN);
}
else if (cTimeLong > dTimeLong){
txtDueDate.setTextColor(Color.RED);
}
}
}
http://i.stack.imgur.com/bt4Z8.png
不明白,如果其他条件什么ü正在做???你可以解释.. – viv 2010-12-20 10:15:41
我的数据库存储截止日期为Unix纪元时间格式,自1970年1月1日以来的秒数。SQL还可以很容易地获得Unix时代的当前时间,这可以很容易地比较两个日期和找出哪一个更老。 – user548369 2010-12-20 12:46:28