滚动后 ListView 未显示正确的值

2024-07-04

在我的应用程序中我使用的是CustomListViewArrayAdapter显示不同国家的时间。但在 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;
    }

有谁能够帮助我?


ListViews 回收视图,这意味着首先从 XML 扩充一组基本列表条目。当您现在向下滚动时,一个列表条目会隐藏在顶部,而一个新条目会显示在底部。此时此刻getView()使用非空参数调用convertView,因为已经膨胀的视图被重用。

在您的情况下,这意味着整个布局膨胀/设置被跳过(the if (v == null) tree)。这很好,基本上你所要做的就是更新第二个 if 部分中的时间戳(o != null).

它应该包含与此类似的内容,就像您对文本视图所做的那样:

CustomAnalogClock customAC = (CustomAnalogClock) v.findViewById(R.id.yourclockid);
customAC.setTime(o.getOrderTime());

这意味着你必须分配一个ID(通过使用setId())将其添加到布局中时,还必须有一个setTime()方法准备好了。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

滚动后 ListView 未显示正确的值 的相关文章

随机推荐