无法从适配器中的getView返回两个不同的视图

时间:2013-02-28 15:16:24

标签: android android-adapter

开发我的第一个应用程序我正在尝试在列表视图中向我的元素添加部分。所有项目都有一个日期,我希望每次日期更改时返回一个包含日期的简单布局。在我的适配器中,我有以下内容:

    public View getView(int position, View convertView, ViewGroup parent) {
    Match match = matchArrayList.get(position);
    Calendar matchTime = match.getDate();
    SimpleDateFormat date = new SimpleDateFormat("dd-MM-yyyy");
    SimpleDateFormat time = new SimpleDateFormat("HH:mm"); 
    String sDate = date.format(matchTime.getTime());
    SeparatorHolder separatorHolder = null;
    MatchHolder matchHolder = null;

    if (convertView == null) {
        if (!sDate.equals(_lastDate)) {
            convertView = inflator.inflate(R.layout.date_separator, null);
            separatorHolder = new SeparatorHolder();
            separatorHolder.Date = (TextView) convertView.findViewById(R.id.date);
            convertView.setTag(separatorHolder);
        } else {
            convertView = inflator.inflate(R.layout.match_layout, null);
            matchHolder = new MatchHolder();
            matchHolder.Time = (TextView) convertView.findViewById(R.id.time);
            matchHolder.HomeTeam = (TextView) convertView.findViewById(R.id.homeTeam);
            matchHolder.AwayTeam = (TextView) convertView.findViewById(R.id.awayTeam);
            matchHolder.HomeTeamImage = (ImageView) convertView.findViewById(R.id.homeTeamImageView);
            matchHolder.AwayTeamImage = (ImageView) convertView.findViewById(R.id.awayTeamImageView);
            matchHolder.TournamentImage = (ImageView) convertView.findViewById(R.id.tournamentImageView);
            matchHolder.TVChannelImage = (ImageView) convertView.findViewById(R.id.tvChannelImageView);
            convertView.setTag(matchHolder);
        }
    } 
    else {
        if (!sDate.equals(_lastDate)) 
            matchHolder = (MatchHolder) convertView.getTag();
        else
            separatorHolder = (SeparatorHolder) convertView.getTag();
    }

    if (!sDate.equals(_lastDate)) {
        _lastDate = sDate;
        separatorHolder.Date.setText(sDate);
    } else {
        UrlImageViewHelper.setUrlDrawable(matchHolder.TournamentImage, match.getTournamentImage());
        UrlImageViewHelper.setUrlDrawable(matchHolder.HomeTeamImage, match.getHomeTeamImage());
        matchHolder.HomeTeam.setText(match.getHomeTeam());
        UrlImageViewHelper.setUrlDrawable(matchHolder.AwayTeamImage, match.getAwayTeamImage());
        matchHolder.AwayTeam.setText(match.getAwayTeam());
        matchHolder.Time.setText(time.format(matchTime.getTime()));
        UrlImageViewHelper.setUrlDrawable(matchHolder.TVChannelImage, match.getTVChannelImage());
    }

    return convertView;
}

一切正常,直到我到达最后一行:

return convertView;

在该行添加断点并尝试立即传递它会引发某种错误。

现在是Android和Eclipse的新手我真的找不到类似于我在Visual Studio中编写.Net时获得的堆栈跟踪。我只能看到,在Eclipse的Debug透视图中,打开了一个带有AbsListView.class的选项卡......

任何人都可以理解我想要实现的目标吗?也许有点帮忙?我看了this link,据我所知,他还会根据项目类型返回两个不同的视图: - ?

编辑: 已实施的Bostons街道回答并将代码更改为

@Override
public int getItemViewType(int position) {
    Match match = matchArrayList.get(position);
    if (match.HomeTeam == "") {
        return 0;
    }
    else {
        return 1;
    }
}

现在它可以工作,但在我的列表中滚动我的应用程序

提前致谢

2 个答案:

答案 0 :(得分:12)

您的问题中没有足够的信息。但是,我会尽我所能: - )

如果您返回多个(类型)ViewR.layout.date_separatorR.layout.match_layout),则必须实施适配器的getViewTypeCountgetItemViewType方法:

@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public int getItemViewType(int position) {
    Match match = matchArrayList.get(position);
    ...
    ...
    if (!sDate.equals(_lastDate)) {
        return 0; // matches R.layout.date_separator
    }
    else {
        return 1; // matches R.layout.match_layout
    }
}

它与视图的回收有关。在convertView != null时,您必须确保参数convertView与之前完成的convertView的原始通胀相匹配。方法getItemViewType确保了这一点。

以下是建议的 getView实施:

  • convertView的通胀并非完全由getItemViewType
  • 驱动
  • 在第二个if语句中切换if和else(SeparatorHolderMatchHolder切换到调用convertView.getTag

public View getView(int position, View convertView, ViewGroup parent) {
    Match match = matchArrayList.get(position);
    Calendar matchTime = match.getDate();
    SimpleDateFormat date = new SimpleDateFormat("dd-MM-yyyy");
    SimpleDateFormat time = new SimpleDateFormat("HH:mm"); 
    String sDate = date.format(matchTime.getTime());
    SeparatorHolder separatorHolder = null;
    MatchHolder matchHolder = null;

    int itemType = getItemViewType(position);
    if (convertView == null) {
    if (itemType == 0) {
        convertView = inflator.inflate(R.layout.date_separator, null);
        separatorHolder = new SeparatorHolder();
        separatorHolder.Date = (TextView) convertView.findViewById(R.id.date);
        convertView.setTag(separatorHolder);
    } else {
        convertView = inflator.inflate(R.layout.match_layout, null);
        matchHolder = new MatchHolder();
        matchHolder.Time = (TextView) convertView.findViewById(R.id.time);
        matchHolder.HomeTeam = (TextView) convertView.findViewById(R.id.homeTeam);
        matchHolder.AwayTeam = (TextView) convertView.findViewById(R.id.awayTeam);
        matchHolder.HomeTeamImage = (ImageView) convertView.findViewById(R.id.homeTeamImageView);
        matchHolder.AwayTeamImage = (ImageView) convertView.findViewById(R.id.awayTeamImageView);
        matchHolder.TournamentImage = (ImageView) convertView.findViewById(R.id.tournamentImageView);
        matchHolder.TVChannelImage = (ImageView) convertView.findViewById(R.id.tvChannelImageView);
        convertView.setTag(matchHolder);
    }
    } 
    else {
        if (itemtype == 0) 
            separatorHolder = (SeparatorHolder) convertView.getTag();
        else
            matchHolder = (MatchHolder) convertView.getTag();
    }

    if (itemType == 0) {
    _lastDate = sDate;
    separatorHolder.Date.setText(sDate);
    } else {
    UrlImageViewHelper.setUrlDrawable(matchHolder.TournamentImage, match.getTournamentImage());
    UrlImageViewHelper.setUrlDrawable(matchHolder.HomeTeamImage, match.getHomeTeamImage());
    matchHolder.HomeTeam.setText(match.getHomeTeam());
    UrlImageViewHelper.setUrlDrawable(matchHolder.AwayTeamImage, match.getAwayTeamImage());
    matchHolder.AwayTeam.setText(match.getAwayTeam());
    matchHolder.Time.setText(time.format(matchTime.getTime()));
    UrlImageViewHelper.setUrlDrawable(matchHolder.TVChannelImage, match.getTVChannelImage());
    }

    return convertView;
}

答案 1 :(得分:0)

我看到问题已关闭,但您应该使用相同的方法进行字符串比较,例如match.HomeTeam == ""。 请查看此answer以获取更多信息