ListView 和记住数据
我对 ListView
有一些问题(这更多是视觉问题),它会记住来自 TextViews
和 ListView
的文本值等数据的行,但混乱或忘记了有关 TextViews
背景颜色的方面。 仅当 ListView
包含许多行时,该问题才会出现。我使用 SimpleCursorAdapter
和 ViewBinder
并希望在发生条件时突出显示 TextView
:
在 ViewBinder
中实现:
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
CharSequence display;
if(view.getId() == R.id.dueDate){
long timestamp = Long.parseLong(cursor.getString(3));
display=DateUtils.getRelativeTimeSpanString(timestamp);
if(timestamp+DaysToMillis(5)> new Date().getTime())
((TextView) view).setBackgroundResource(R.color.marker_red);
((TextView) view).setText(display);
return true;
}
else
return false;
}
同样:当有很多行时,ListView
似乎会混淆背景颜色。有时,当我重新滚动时,它再次更改了 TextView
的背景,而我似乎无法找到错误或 ListView
的逻辑。
那么,每次在膨胀行之前都会调用 setViewValue(..)
方法吗?即使您向上或向下滚动并且不再可见的行再次可见?或者只是为了初始行膨胀过程而调用 setViewValue(..)
方法,然后将创建的对象保留在 ListView
的内存中?
PS 来自同一 TextView
的文本值可以正确显示。我在同一个 setViewValue(..)
方法中使用了相同的逻辑。
I have some problems with a ListView
(it's more of a visual problem), it remembers data such as text values from TextViews
, from the ListView
's rows, but tangles or forgets aspects regarding the TextViews
background color.
The problem shows up only when the ListView
contains many rows. I work with a SimpleCursorAdapter
and a ViewBinder
and want to highlight a TextView
if a condition occurs:
In the ViewBinder
implementation:
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
CharSequence display;
if(view.getId() == R.id.dueDate){
long timestamp = Long.parseLong(cursor.getString(3));
display=DateUtils.getRelativeTimeSpanString(timestamp);
if(timestamp+DaysToMillis(5)> new Date().getTime())
((TextView) view).setBackgroundResource(R.color.marker_red);
((TextView) view).setText(display);
return true;
}
else
return false;
}
So again: when there are many rows the ListView
seems to tangle the background color. Some times when I re-scroll, it change's again the TextView
's background and I can't seem to find the bug or the ListView
's logic.
So, does the setViewValue(..)
method gets called every time before inflating a row? Even if you scroll up or down and a row that's not visible anymore get's visible again? Or the setViewValue(..)
method gets called just for a initial row inflating process and then the created objects are keept in the ListView
's memory?
P.S. The text values from the same TextView
is displayed correctly. I used the same logic in the same setViewValue(..)
method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
同样的新手
在我看来(如果我错了,请纠正我......特别是如果你是罗马人)ViewBinder,及其 setViewValue 方法回收了 listView 的行......
对于显示的每一行的每个“FROM - TO”绑定,即使对于已经膨胀的行,也会调用 setViewValue(..) ,并且通过向上/向下滚动它们会再次显示;
关于我积累的一段代码,对于再次显示的行:
((TextView) view).setText(display);
- 将设置正确的内容,相同的时间戳将被处理...但如果它没有通过:if(timestamp+DaysToMillis(5)> new Date().getTime())
((TextView)视图).setBackgroundResource(R.color.marker_red);
- 当前的 TextView 可能具有来自回收的 TextView 的背景
无论如何,我必须添加一个
else ((TextView) view).setBackgroundResource(0);
到我的if< /code>
Same newbie
It seems to me (and if I'm wrong please correct me... especially if you are Roman Guy) that ViewBinder, with it's setViewValue method recycles the listView's rows...
setViewValue(..) gets called for every "FROM - TO" binding for every row displayed, even for rows that were already inflated, and by scrolling up/down they got displayed again;
and regarding my amassing piece of code, for a row that gets displayed again:
((TextView) view).setText(display);
- will set the correct content, same timestamp will get processed... but if it doesn't passes the:if(timestamp+DaysToMillis(5)> new Date().getTime())
((TextView) view).setBackgroundResource(R.color.marker_red);
-the current TextView might have for background the background from a recycled TextView
anyways I had to add an
else ((TextView) view).setBackgroundResource(0);
to myif