1

カスタム アダプターを備えた ListView があります。すべての行には、特定の制約の下でのみ表示される ImageView があります。問題は、最初の行にこの ImageView が表示されている場合、最後の行にも表示され、その逆もあることです。

getView()これが私のアダプターのコードです。

public View getView(int position, View view, ViewGroup parent) {
    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.row_idea, null);
    }

    Idea idea = mIdeas.get(position);

    if (idea != null) {
        ImageView imgAlarm = (ImageView) view
                .findViewById(R.id.imgAlarm_rowIdea);
        if (idea.getTimeReminder() != null)
            imgAlarm.setVisibility(ImageView.VISIBLE);

        TextView lblTitle = (TextView) view
                .findViewById(R.id.lblTitle_rowIdea);
        lblTitle.setText(idea.getTitle());

        TextView lblDescription = (TextView) view
                .findViewById(R.id.lblDescription_rowIdea);
        lblDescription.setText(idea.getDescription());
    }
    return view;
}

mIdeasArrayList表示するすべてのデータを含むListViewです。 imgAlarm私が上で言ったImageViewことです。

4

2 に答える 2

2

ImageView条件が満たされない場合の可視性ステータスを元に戻す必要があるため、再利用された可能性のあるビューに問題が発生することはありません (ImageView既に表示されている可能性があり、表示されるべきではない場合に表示される可能性があります)。

if (idea.getTimeReminder() != null) {
     imgAlarm.setVisibility(ImageView.VISIBLE);
} else {
    imgAlarm.setVisibility(ImageView.INVISIBLE); // or GONE
}
于 2013-01-19T20:34:34.070 に答える
2

変化する

if (idea.getTimeReminder() != null)
            imgAlarm.setVisibility(ImageView.VISIBLE);

if (idea.getTimeReminder() != null)
            imgAlarm.setVisibility(ImageView.VISIBLE);
else
            imgAlarm.setVisibility(ImageView.GONE);

ここで起こっていることは、アダプターがビューを「リサイクル」していることです。したがって、テストで見ている場合、最後のビューと最初のビューは実際には同じインスタンスです。

于 2013-01-19T20:34:57.553 に答える