• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Custom ListView repeats itself when scrolling

xfj37dsj3b

Lurker
Jul 18, 2010
5
0
I have a custom listview that pulls from an array, however when I reach a point that I have to scroll down to see more items, the items start to repeat themselves. According to the array, the items are fine and not repeating themselves, it looks like it's just the ListAdapter. Any help is greatly appreciated. I think the getView is really the only problem, but if you need other parts of the code, let me know.
Code:
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.list_item, parent, false);
			holder = new ViewHolder();
			holder.title = (TextView) convertView.findViewById(R.id.row_title);
			holder.summary = (TextView) convertView.findViewById(R.id.row_summary);
			holder.url = (TextView) convertView.findViewById(R.id.row_link);
			convertView.setTag(holder);
			String item = CurbAlert.listings[position];
			item = item.replace("<br>", "");
			String title = item.substring(item.indexOf("<title>") + 16, item.indexOf("</title>") - 3);
			String summary = item.substring(item.indexOf("<description>") + 22, item.indexOf("</description>") - 3);
			if (summary.length() > 100) { summary = summary.substring(0, 100); summary += "..."; }
			holder.title.setText(title);
			holder.summary.setText(summary);
			holder.url.setText(item.substring(item.indexOf("<link>") + 6, item.indexOf("</link>")));
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		return convertView;
	}
 
The first time through the getView method the convertView is empty, but when you scroll, it reuses that convertView (it creates just enough convertViews to fill the listview's space). So, your else statement is the only thing that gets executed and it sends that same convertView back out with the data that was in it before.

To fix it; it looks like you need to move everything after "holder = new ViewHolder();" in the if block to below the if else block.
 
Upvote 0
hey,can you send me the complete source for that repeating items in listview..
thanks in advance..

  1. public View getView(int position, View convertView, ViewGroup parent) {
  2. ViewHolder holder;
  3. if (convertView == null) {
  4. convertView = mInflater.inflate(R.layout.list_item, parent, false);
  5. holder = new ViewHolder();
  6. } else {
  7. holder = (ViewHolder) convertView.getTag();
  8. }
  9. holder.title = (TextView) convertView.findViewById(R.id.row_title);
  10. holder.summary = (TextView) convertView.findViewById(R.id.row_summary);
  11. holder.url = (TextView) convertView.findViewById(R.id.row_link);
  12. convertView.setTag(holder);
  13. String item = CurbAlert.listings[position];
  14. item = item.replace("<br>", "");
  15. String title = item.substring(item.indexOf("<title>") + 16, item.indexOf("</title>") - 3);
  16. String summary = item.substring(item.indexOf("<description>") + 22, item.indexOf("</description>") - 3);
  17. if (summary.length() > 100) { summary = summary.substring(0, 100); summary += "..."; }
  18. holder.title.setText(title);
  19. holder.summary.setText(summary);
  20. holder.url.setText(item.substring(item.indexOf("<link>") + 6, item.indexOf("</link>")));
  • return convertView;
  • }
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones