1

最初のアプリを開発して、リスト ビューの要素にセクションを追加しようとしています。すべてのアイテムには日付があり、日付が変わるたびに日付を含む単純なレイアウトを返したいと考えています。私のアダプターには次のものがあります。

    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のタブが開かれていることだけです...

誰でも私が達成しようとしていることを理解できますか? で、ちょっと手伝って?私はこのリンクを見ましたが、私が知る限り、彼はアイテムタイプに応じて2つの異なるビューも返します:-?

編集: Streets Of Bostons の回答を実装し、コードを次のように変更しました

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

今は動作しますが、リスト内でスクロールします FC のマイ アプリ

前もって感謝します

4

2 に答える 2

12

質問に十分な情報がありません。しかし、私は最善を尽くします:-)

複数の(タイプ)ViewR.layout.date_separatorまたはR.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
  • 2番目のifステートメントでifとelseを切り替えました(SeparatorHolder呼び出しMatchHolderで切り替えました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;
}
于 2013-02-28T15:20:27.387 に答える
0

質問はクローズされているようですが、このような文字列比較には equal メソッドを使用する必要がありますmatch.HomeTeam == ""。詳細については、この回答をご覧ください

于 2013-02-28T17:01:23.567 に答える