滚动后 ListView 未显示正确的值 [英] ListView is not showing correct values after scrolling

查看:20
本文介绍了滚动后 ListView 未显示正确的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用带有 ArrayAdapterCustomListView 来显示不同国家/地区的时间.但是在 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.

根据之前的一些帖子,我编写了以下代码片段来获得解决方案.但问题仍然存在.

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?

推荐答案

ListViews 回收视图,这意味着首先从 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.

在您的情况下,这意味着跳过整个布局膨胀/设置(if (v == null) 树).这很好,基本上你所要做的就是更新第二个 if 部分的时间戳 (o != 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).

它应该包含与此类似的内容,就像您对 textview 所做的一样:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆