getView位置变量奇怪的值

时间:2011-05-10 21:45:19

标签: android listview

我正在尝试创建一个showlist,正在使用的布局必须取决于日期。

基本上,我只想在列表的开头显示项目的日期,或者该值是否与上一项目的日期不同。

我将日期存储为String数据库(在这种情况下更容易)

我得到了相当奇怪的结果,所以我检查了'position'变量的值,并且出于某种原因,在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 个答案:

答案 0 :(得分:1)

每次尝试充气视图。如果有人有更好的解决方案,请提供。

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

答案 1 :(得分:0)

你的ListView是否在ScrollView中,我一直在努力解决相同的问题,现在我只注意到我的ListView在ScrollView中,因为我最近将我的布局从原始的LinearLayout更改为ListView所以我可以解决一些性能问题。在Google I / O的视频“World of ListView”上,ListView的开发人员对此问题发表评论,禁止进行此组合。我从ScrollView中删除了ListView,而ta-da,现在工作正常。没有更多的位置0。