在我的申请中,我使用CustomListView
和ArrayAdapter
来显示不同国家/地区的时间。但是在6到7行之后(取决于手机屏幕尺寸),时间值会重复。
根据之前的一些帖子,我编写了以下代码片段来获得解决方案。但问题仍然存在。
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
Order o = items.get(position);
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = (LinearLayout) vi.inflate(R.layout.row, null);
CustomDigitalClock customDC = new CustomDigitalClock(CityList.this, o.getOrderTime());
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT);
customDC.setTextColor(Color.WHITE);
customDC.setTextSize(13);
LayoutParams param=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
ll.addView(customDC, 2);
v = ll;
}
if (o != null) {
TextView tt = (TextView) v.findViewById(R.id.toptext);
TextView bt = (TextView) v.findViewById(R.id.bottomtext);
if (tt != null) {
tt.setText("" + o.getOrderName());
}
if (bt != null) {
bt.setText("" + o.getOrderStatus());
}
v.setOnCreateContextMenuListener(this);
}
return v;
}
有人能帮助我吗?
答案 0 :(得分:3)
ListViews回收视图,这意味着首先从XML中扩展一组基本列表条目。当你向下滚动时,一个列表条目隐藏在顶部,一个新列表条目显示在底部。此时getView()
使用非空参数convertView
调用,因为已经膨胀的视图被重用。
在您的情况下,这意味着跳过整个布局通胀/设置(if (v == null)
树)。哪个没问题,基本上所有你要做的就是在第二个if部分(o != null
)中更新时间戳。
它应该包含与此类似的内容,就像你对textviews一样:
CustomAnalogClock customAC = (CustomAnalogClock) v.findViewById(R.id.yourclockid);
customAC.setTime(o.getOrderTime());
这意味着您必须在将视图添加到布局时为您的视图分配ID (使用setId()
),并且还必须使用setTime()
方法准备好了。