综合技术

ListView items do not refresh properly

微信扫一扫,分享到朋友圈

ListView items do not refresh properly
0 0

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!

Need to know the correct syntax of a count query to count rows by ID

上一篇

20 Typographic PSD Mug & Cup Mockup Templates

下一篇

您也可能喜欢

评论已经被关闭。

插入图片
ListView items do not refresh properly

长按储存图像,分享给朋友