ListView is not showing correct values after scrolling

▼魔方 西西 提交于 2019-12-20 14:44:05

问题


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.

Following is the code I have written:

 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?


回答1:


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.

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).

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());

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.



来源:https://stackoverflow.com/questions/8623984/listview-is-not-showing-correct-values-after-scrolling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!