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!阅读原文】。感谢您的支持!

您可能感兴趣的

Listview scroll to the bottom Swing: Scroll to the bottom of JScrollPane, subject to the location of the current window I am attempting to mimic the functiona...
Displaying a ListView item from Page1 on Page2 – P... Just joining the series? Here’s a recap: part 1 – Sara can has MVVM part 2 – Databinding a List View part 3 – Displaying an ItemDetai...
Kivy: Horizontal ListView Is there posibility to create in Kivy framework horizontal ListView? All examples shows how to create a list of elements with vertical orientation, ...
Listview setEnabled () causing a... I have a list view inside a dialog fragment. The list view works fine and is being set in onActivityCreated() . E.g. SimpleAdapter adapter = n...
Flutter 中 ListView 的使用 这个小例子使用的是豆瓣 API 中 正在上映的电影 的开放接口,要实现的主要效果如下: JSON 数据结构 Item 结构 Item 的结构是一个 Card 包含着一个 Row 然后这个 Row 里面左边是一个 Image ,右边是一个 Co...