ListView recycles item in wrong position when keyboard is hidden
I have a really weird bug, that I cannot understand. Here is the behavior in video form http://www.youtube.com/watch?v=hHuUzNDI68w
Now to explain. I have an activity, called NewsFeed. which consists of a ListView of feed items (each has a photo that comes from a url, which I load using a caching class, and some text on the side) if I open this activity normaly, everything is alright, when I scroll around everything works as expected. If I however first press the search key, wherein the key开发者_运维问答board comes up, and I select something from the search results, the NewsFeed activity opens At first when it shows up, the keyboard is still up, and then a split second later the keyboard goes down the end result is that the 2 images on top are identical (they shouldn't be)
Through some debugging I have figured out that for some reason the viewHolder for the top 2 rows, ends up being the same object on the second go round (once the keyboard is finally gone)
public View getView (int position, View convertView, ViewGroup parent) {
final HashMap<String, Object> game = getItem(position);
CollapsedNewsHolder collapsedNewsHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.news_collapsed_module, null);
collapsedNewsHolder = new CollapsedNewsHolder();
collapsedNewsHolder.newsPicture = (ImageView) convertView.findViewById(R.id.NewsPicture);
collapsedNewsHolder.progress = (ProgressBar) convertView.findViewById(R.id.Progress);
collapsedNewsHolder.title = (TextView) convertView.findViewById(R.id.title);
collapsedNewsHolder.newsPerson = (TextView) convertView.findViewById(R.id.newsPerson);
collapsedNewsHolder.upCount = (TextView) convertView.findViewById(R.id.upCount);
collapsedNewsHolder.commentCount = (TextView) convertView.findViewById(R.id.commentCount);
collapsedNewsHolder.relativeTime = (TextView) convertView.findViewById(R.id.RelativeTime);
collapsedNewsHolder.setTypeface();
//collapsedNewsHolder.imageCallback = new MyImageCallback(mActivity, collapsedNewsHolder.newsPicture, collapsedNewsHolder.progress, MyImageCallback.SMALL, 200);
//collapsedNewsHolder.itemClickListener = new ItemClickListener();
convertView.setTag(collapsedNewsHolder);
} else
collapsedNewsHolder = (CollapsedNewsHolder) convertView.getTag();
if ((Integer) getItem(position).get("type") != NewsFeed.POSTER)
collapsedNewsHolder.title.setTextSize(13f);
collapsedNewsHolder.newsPicture.setImageBitmap(null);
collapsedNewsHolder.progress.setVisibility(View.VISIBLE);
ImageUtil.imageCache.loadAsync((String) game.get("img"), new MyImageCallback(mActivity, collapsedNewsHolder.newsPicture, collapsedNewsHolder.progress, MyImageCallback.SMALL, 200), mContext);
collapsedNewsHolder.title.setText((String) game.get("title"));
collapsedNewsHolder.newsPerson.setText((String)game.get("newsPerson"));
collapsedNewsHolder.relativeTime.setText((String) game.get("relativeTime"));
collapsedNewsHolder.upCount.setText((Integer) game.get("upCount") + "");
collapsedNewsHolder.commentCount.setText((Integer) game.get("commentCount") + "");
//convertView.setOnClickListener(collapsedNewsHolder.itemClickListener.set((String) game.get("id"), position));
convertView.setOnClickListener(new ItemClickListener((String) game.get("id"), position));
return convertView;
}
Also here is an error log that illustrates the problem. Note the memory locations and the image urls, in regards to the position in the list.
05-05 17:34:54.774: ERROR/NewsFeedAdapter(7101): Position- 0 Image- android.widget.ImageView@460977f8 Loading image- http://lh3.ggpht.com/o0vA6-MRGAXhhYYkPa1xf3WsPlDe4XnK2xWZeMhmXKBQI8eHGTAos0l1zqC_nox2MrRHKcCKNnPgzqLctsqh25e3LMc=s500
05-05 17:34:54.804: ERROR/NewsFeedAdapter(7101): Position- 1 Image- android.widget.ImageView@46142f28 Loading image- http://lh6.ggpht.com/RcTe-3O-Rywdp0zizVx6k6lSEuPpYbDYnu1UoEnWbhLk6BzwzXt29BvK5rMPAqkrhgtvxvddSTL3pBnMDeLIXVV3s0EZng=s500
05-05 17:34:55.154: ERROR/NewsFeedAdapter(7101): Position- 0 Image- android.widget.ImageView@46142f28 Loading image- http://lh3.ggpht.com/o0vA6-MRGAXhhYYkPa1xf3WsPlDe4XnK2xWZeMhmXKBQI8eHGTAos0l1zqC_nox2MrRHKcCKNnPgzqLctsqh25e3LMc=s500
05-05 17:34:55.174: ERROR/NewsFeedAdapter(7101): Position- 1 Image- android.widget.ImageView@460977f8 Loading image- http://lh6.ggpht.com/RcTe-3O-Rywdp0zizVx6k6lSEuPpYbDYnu1UoEnWbhLk6BzwzXt29BvK5rMPAqkrhgtvxvddSTL3pBnMDeLIXVV3s0EZng=s500
05-05 17:34:55.304: ERROR/NewsFeedAdapter(7101): Position- 2 Image- android.widget.ImageView@4615cd28 Loading image- http://lh4.ggpht.com/V95vJI4WHUUluC8_jHaJw011U6RO7I9rVBQsdSvwDrnUrOCN2YvqhHftr7o0YMNv8cIEBEjsnLNvIH0Y1KoB-rzw6hrKiQ=s500
05-05 17:34:55.334: ERROR/NewsFeedAdapter(7101): Position- 3 Image- android.widget.ImageView@46061610 Loading image- http://lh3.ggpht.com/-c7Ss0t2luOD-npAFZcjRYbnuasqjUNTOv75pFhyprhyzGDf8QdIz_PrJS5qcmBb0a5xrXzZWZYH7oc06eo=s500
05-05 17:34:55.384: ERROR/NewsFeedAdapter(7101): Position- 4 Image- android.widget.ImageView@4613d058 Loading image- http://lh3.ggpht.com/iCKp7kwcnbnZGD4vBb-Mz_mtSKPvST-YGeekJ9khljpG-Ua5Kl71ETdn8LLulULmq9fgNy8huooi_CfrT4W3gjqoFhiB=s500
I had a similar problem. I solved it by setting android:windowSoftInputMode
to adjustPan
.
精彩评论