ListView items do not refresh properly

综合技术 2018-06-17 阅读原文

I have created a custom ArrayAdapter to display data within ListView items from an array of data structures and for some reason when I scroll up and down really quickly the display will do strange things.

Here is my getView function from the custom ArrayAdapter. My first guess would be because of the if (d.highTemp != null) is causing lag or something? What I want the adapter to do is only display a highTemp if the data structure has a value within it. Otherwise it should display the lowTemp value; The list is correct when initially drawn but if I scroll up and down really fast it will start displaying both high and lowTemp values even though I know each data structure only has one of those != null...

@Override
public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list_item, null);
        }
        DaysWeather d = daysWeather[position];

        if (d != null) {
                TextView tt = (TextView) v.findViewById(R.id.toptext);
                TextView bt = (TextView) v.findViewById(R.id.bottomtext);
                TextView trt = (TextView) v.findViewById(R.id.toprighttext);
                TextView tbt = (TextView) v.findViewById(R.id.bottomrighttext);

                if (tt != null) {
                      tt.setText(d.name);}
                if(bt != null){
                      bt.setText(d.shortDescription);
                }
                if (d.highTemp != null){
                    if (trt != null) {
                        trt.setText("Hi: " + d.highTemp);
                        tbt = null;
                    }
                } else {
                    if (tbt != null) {
                        trt = null;
                        tbt.setText("Lo: " + d.lowTemp);

                    }
                }
        }
        return v;
}

Since you are reusing any given row's view, that row may already have the Hi temp text view set, so you need to explicitly set the field that you don't want to an empty string (or hidden).

You need to do something like this:

if (d.highTemp != null) {
    if (trt != null) {
       trt.setText("Hi: " + d.highTemp);
       tbt.setText(""); //set this field to blank
       tbt = null;
    }
} else {
    if (tbt != null) {
       tbt.setText("Lo: " + d.lowTemp);
       trt.setText(""); //set this field to blank
       trt = null;
    }
}
Hello, buddy!

责编内容by:Hello, buddy!阅读原文】。感谢您的支持!

您可能感兴趣的

Xamrin.Forms 用户界面——控件——ListView——ListView 交互... ListView交互性 通过实施选择,滑动删除和拉式刷新,将交互功能添加到您的ListView。 PDF用于离线使用 下载PDF ...
ScrollView嵌套ListView,ListView分页加载数据问题... 在开发中,遇到一个问题。ScrollView里嵌套一个ListView。大多数的做法是重写一个MyListView来解决。 public class MyListView extends ListView{publi...
源码解析之ListView 大家元旦快乐~ 好记性不如烂笔头,所以我准备弄个源码解析系列,不准备详细解析源码,但把基本原理和设计思想梳理清楚,也给自己留个笔记存档好在后面需要的时候翻起。 今天就从ListView开始。 ListView的核心在于layou...
Flutter 18: 图解 ListView 下拉刷新与上拉加载 (二)【Notification... 小菜上次尝试 ListView 异步加载列表数据时,用了三方库 flutter_refresh ,这种方式使用很简单。但列表数据的加载也绝非一种,小菜这次准备用原生尝试一下。因为种种原因,小菜这次的整理距离上次时间很长,还是应该加强自...
UWP 和 WPF 不同,ListView 中绑定的集合修改顺序时,UI 的刷新规则... ObservableCollection 中有一个 Move 方法,而这个方法在其他类型的集合中是很少见的。由于 ObservableCollection 主要用于绑定,涉及到 UI 更新,而 UI 更新普遍比普通的集合修改慢了不止一个数...