2011-05-10 48 views
0

我想創建一個showlist,並且正在使用的佈局必須取決於日期。getView位置變量奇怪的值

基本上,我只想在列表的開頭顯示一個項目的日期,或者如果該值不同於前一個項目的日期。

我存儲的日期作爲字符串數據成員(更容易在這種情況下)

,我十分奇怪的結果,所以我檢查了「位置」變量的值,以及由於某種原因後5左右,它會回到0並顯示一些隨機數字。我不知道這是爲什麼,也許有人可以幫助我?

這是我有:

private LayoutInflater li = LayoutInflater.from(this.getContext()); 

    private String currentDate; 

    public EpisodeListAdapter(){ 
     super(UnwatchedEpisodesActivity.this, 0, episodeList); 

     currentDate = episodeList.get(0).getAirDate().getRawDateString(); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     Log.d(AppConstants.APP_NAME, "pos:" + position); 
     if(position == 0){ 
      Log.e(AppConstants.APP_NAME, "A"); 

      if(convertView == null){ 
       convertView = li.inflate(R.layout.episode_date, parent, false); 
      } 
      ((TextView) convertView.findViewById(R.id.date1)).setText(episodeList.get(position).getAirDate().getRawDateString()); 
      ((TextView) convertView.findViewById(R.id.text3)).setText(episodeList.get(position).getTitle()); 
      ((TextView) convertView.findViewById(R.id.text4)).setText(episodeList.get(position).getShowName()); 
     } 
     else if(currentDate != episodeList.get(position).getAirDate().getRawDateString()){ 
      Log.e(AppConstants.APP_NAME, "B"); 

      if(convertView == null){ 
       convertView = li.inflate(R.layout.episode_date, parent, false); 
      } 
      ((TextView) convertView.findViewById(R.id.date1)).setText(episodeList.get(position).getAirDate().getRawDateString()); 
      ((TextView) convertView.findViewById(R.id.text3)).setText(episodeList.get(position).getTitle()); 
      ((TextView) convertView.findViewById(R.id.text4)).setText(episodeList.get(position).getShowName()); 

      currentDate = episodeList.get(position).getAirDate().getRawDateString(); 
     } 
     else { 
      Log.e(AppConstants.APP_NAME, "C"); 

      if(convertView == null){ 
       convertView = li.inflate(R.layout.episode_row, parent, false); 
      } 
      ((TextView) convertView.findViewById(R.id.text1)).setText(episodeList.get(position).getTitle()); 
      ((TextView) convertView.findViewById(R.id.text2)).setText(episodeList.get(position).getShowName()); 

      currentDate = episodeList.get(position).getAirDate().getRawDateString(); 

     } 

     return convertView; 
    } 

回答

2

嘗試每次充氣視圖。如果有人有更好的解決方案,請提供。

private LayoutInflater li = LayoutInflater.from(this.getContext());

private String currentDate; 

public EpisodeListAdapter(){ 
    super(UnwatchedEpisodesActivity.this, 0, episodeList); 

    currentDate = episodeList.get(0).getAirDate().getRawDateString(); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    Log.d(AppConstants.APP_NAME, "pos:" + position); 
    if(position == 0){ 
     Log.e(AppConstants.APP_NAME, "A"); 

      convertView = li.inflate(R.layout.episode_date, parent, false); 

     ((TextView) convertView.findViewById(R.id.date1)).setText(episodeList.get(position).getAirDate().getRawDateString()); 
     ((TextView) convertView.findViewById(R.id.text3)).setText(episodeList.get(position).getTitle()); 
     ((TextView) convertView.findViewById(R.id.text4)).setText(episodeList.get(position).getShowName()); 
    } 
    else if(currentDate != episodeList.get(position).getAirDate().getRawDateString()){ 
     Log.e(AppConstants.APP_NAME, "B"); 

      convertView = li.inflate(R.layout.episode_date, parent, false); 

     ((TextView) convertView.findViewById(R.id.date1)).setText(episodeList.get(position).getAirDate().getRawDateString()); 
     ((TextView) convertView.findViewById(R.id.text3)).setText(episodeList.get(position).getTitle()); 
     ((TextView) convertView.findViewById(R.id.text4)).setText(episodeList.get(position).getShowName()); 

     currentDate = episodeList.get(position).getAirDate().getRawDateString(); 
    } 
    else { 
     Log.e(AppConstants.APP_NAME, "C"); 

      convertView = li.inflate(R.layout.episode_row, parent, false); 

     ((TextView) convertView.findViewById(R.id.text1)).setText(episodeList.get(position).getTitle()); 
     ((TextView) convertView.findViewById(R.id.text2)).setText(episodeList.get(position).getShowName()); 

     currentDate = episodeList.get(position).getAirDate().getRawDateString(); 

    } 

    return convertView; 
} 
0

是滾動型內你的ListView,我一直在用了一段時間同樣的問題現在掙扎,只是現在我發現我的ListView是一個滾動型裏面,因爲我最近更改佈局從原始的LinearLayout來一個ListView,所以我可以解決一些性能問題。在來自Google I/O的視頻「World of ListView」中,ListView的開發人員評論這個問題,禁止這種組合。我從ScrollView和ta-da中刪除了ListView,它現在工作正常。沒有更多的位置0.