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! (源链)。感谢您的支持!

您可能感兴趣的

WinJS.UI.ListView – re... I've got a ListView that was using HTML-defined templates like this: ...
Xamarin.Forms 用户界面——控件——ListView——ListView 性能... ListView性能 使用基于ListView的应用程序确保卓越的性能。 PDF用于离线使用 ...
Linking WPF to ListView SelectedItem Does Not Work I'm using a ListView to display contents of a log in my application. I want to c...
JavaFX ListView Cell Bidirectional Between Graph a... I managed to make working ListView s by overriding updateItem(T item,bool...
打造一个无数据显示无数据提示信息的ListView... 最近我们测试人员说,我们的所有的列表都要做一个没有数据就提示没有数据,当时我的表情是这样的 =_=!!! 我的天呐……这么多列表真的要一个一个做嘛!!!然后我想...