2010-11-10 44 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