2010-12-20 108 views
0

嘿,大家我不是一个专业的编码员,但我一直在玩一个简单的待办事项列表来学习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); 
    } 
    } 
} 

image http://i.stack.imgur.com/bt4Z8.png

+0

不明白,如果其他条件什么ü正在做???你可以解释.. – viv 2010-12-20 10:15:41

+0

我的数据库存储截止日期为Unix纪元时间格式,自1970年1月1日以来的秒数。SQL还可以很容易地获得Unix时代的当前时间,这可以很容易地比较两个日期和找出哪一个更老。 – user548369 2010-12-20 12:46:28

回答

0

最简单的解决办法是添加以下txtDueDate.setText("Not Set");后:

txtDueDate.setTextColor(Color.BLACK); 

的原因是ListView回收您在newView方法创建View对象。

所以当bindView被调用时,v参数并不总是一个闪亮的新View对象,但实际上一个从列表的顶部是不可见的更多,所以以前有绿色或红色文本。

因此,您需要确保您明确设置了所需的所有属性,包括文本颜色。

除此之外,还可以进一步优化。例如,您不应该在newView方法—中调用setText,您应该只在绑定方法中执行此操作。

有关如何在ListView的作品,你可以看这个谈话或看PDF幻灯片一些好的信息:
http://www.google.com/events/io/2010/sessions/world-of-listview-android.html

+0

这样做!我每隔一天都会为此花费两三个小时的时间来想象我终究会找到答案,但我没有弄清楚为什么会发生这种情况。回收视图回答了很多关于ListViews的问题,谢谢。这是我在这里的第一篇文章,是否有一种方法我应该关闭它或标记它解决? – user548369 2010-12-20 12:42:37

+0

你可以接受这位用户的回答...... – viv 2010-12-20 12:50:33