2013-01-19 21 views
1

我有一个带有自定义适配器的ListView。在每一行都有一个ImageView,只有在一定的限制条件下才可见。问题是,如果第一行有这个ImageView可见,那么它也是最后一行,反之亦然。ListView中最后一项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

回答

2

变化

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

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

这里发生的事情是适配器是“回收”的意见。所以在你看到你的测试中,最后一个视图和第一个视图实际上是同一个实例。

+0

好吧,它的工作真的很感谢你! –

2

你要恢复的ImageView的可见性状态,如果条件不满足,所以你不要有一个潜在的回收视图的问题(其中可能有ImageView已经可见,并且出现时,它不应该) :

if (idea.getTimeReminder() != null) { 
    imgAlarm.setVisibility(ImageView.VISIBLE); 
} else { 
    imgAlarm.setVisibility(ImageView.INVISIBLE); // or GONE 
}