滚动后的ListView没有显示正确的值 [英] ListView is not showing correct values after scrolling
问题描述
在我的应用程序正在使用 CustomListView
与 ArrayAdapter
来显示不同国家的时间。但后6至7行(取决于电话屏幕大小)值重复的时间。
In my application I am using a CustomListView
with an ArrayAdapter
to show different countries time. But after 6 to 7 rows(depending on the phone screen size) the time values are repeating.
据一些previous后我写了下面的code段,以获得解决方案。但问题依然存在。
According to some previous post I have written the following code snippet to get the solution. But the problem is still there.
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;
}
任何人可以帮助我吗?
Can anybody help me?
推荐答案
列表视图回收的观点,即在第一个列表条目的基础组从XML膨胀。现在,当您向下滚动,一个列表条目被隐藏在顶部,和一个新的被显示在底部。这时 getView()
是带一个非空参数 convertView
,因为一个已经膨胀的观点被重复使用。
ListViews recycle views, which means at first a base set of list entries is inflated from XML. When you scroll down now, one list entry gets hidden at the top, and a new one gets shown at the bottom. At this moment getView()
is called with a non-null argument convertView
, because an already inflated view is reused.
在你的情况,这意味着整个布局通胀/设置被跳过的(即如果(V == NULL)
树)的。这是很好的,基本上所有你所要做的是更新的第二时间戳,如果部分的( 0!= NULL
)的。
In your case that means that the whole layout inflation/setup is skipped (the if (v == null)
tree). Which is fine, basically all you have to do is update the timestamp in the second if section (o != null
).
它应该包含与此类似,像你这样的textviews过:
It should contain something similar to this, like you did with the textviews too:
CustomAnalogClock customAC = (CustomAnalogClock) v.findViewById(R.id.yourclockid);
customAC.setTime(o.getOrderTime());
这意味着你必须分配一个ID的(使用 SETID()
)的你的观点,同时将它添加到布局,还必须有一个的setTime()
法准备好了。
This means that you have to assign an ID (by using setId()
) to your view while adding it to the layout, and also have to have a setTime()
method ready.
这篇关于滚动后的ListView没有显示正确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!