2011-05-10 49 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.