2010-11-10 94 views
0

我已经创建了一个自定义的ArrayAdapter来显示数据结构数组中的ListView项目中的数据,并且出于某种原因,当我快速上下滚动时,显示会做出奇怪的事情。ListView项目不能正确刷新

这是我的自定义ArrayAdapter的getView函数。我的第一个猜测是因为if(d.highTemp!= null)导致滞后或什么?如果数据结构中有一个值,我想让适配器执行的操作只是显示一个highTemp。否则,它应该显示lowTemp值;这份名单是正确的,当最初绘制的,但如果我向上和向下滚动真快,它会开始显示高和lowTemp值,即使我知道每个数据结构只有其中的一个!= 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; 
} 

回答

0

由于您重复使用任何给定的行的视图,该行可能已经有喜临门文本视图集,所以你需要明确地设置你不想使用空字符串(或隐藏)的字段。

你需要做这样的事情:

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; 
    } 
} 
+0

这样做!我已经尝试过'tbt = null'和'trt = null',但它不能正常工作,但是使用了空setText!!非常感谢。 – joepetrakovich 2010-11-11 01:59:54

1

ü尝试用光标适配器......它vl更好......

+0

我会考虑它,我的第一个假设是,cursorAdapters是对数据库存储的数据,这是我在没有任何经验。我会仔细研究一下,谢谢。 – joepetrakovich 2010-11-11 02:01:05