1. Introducing Channels - a new way to chat with other Android users!
    Dismiss Notice

Custom ListView repeats itself when scrolling


Last Updated:

  1. xfj37dsj3b

    xfj37dsj3b Member This Topic's Starter

    Joined:
    Jul 18, 2010
    Messages:
    5
    Likes Received:
    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 (Text):
    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.             holder.title = (TextView) convertView.findViewById(R.id.row_title);
    7.             holder.summary = (TextView) convertView.findViewById(R.id.row_summary);
    8.             holder.url = (TextView) convertView.findViewById(R.id.row_link);
    9.             convertView.setTag(holder);
    10.             String item = CurbAlert.listings[position];
    11.             item = item.replace("<br>", "");
    12.             String title = item.substring(item.indexOf("<title>") + 16, item.indexOf("</title>") - 3);
    13.             String summary = item.substring(item.indexOf("<description>") + 22, item.indexOf("</description>") - 3);
    14.             if (summary.length() > 100) { summary = summary.substring(0, 100); summary += "..."; }
    15.             holder.title.setText(title);
    16.             holder.summary.setText(summary);
    17.             holder.url.setText(item.substring(item.indexOf("<link>") + 6, item.indexOf("</link>")));
    18.         } else {
    19.             holder = (ViewHolder) convertView.getTag();
    20.         }
    21.         return convertView;
    22.     }
     

    Advertisement
  2. galth

    galth Member

    Joined:
    Aug 5, 2010
    Messages:
    12
    Likes Received:
    1
    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.
     
  3. xfj37dsj3b

    xfj37dsj3b Member This Topic's Starter

    Joined:
    Jul 18, 2010
    Messages:
    5
    Likes Received:
    0
    Awesome that worked, thanks a lot!
     
  4. krupa

    krupa New Member

    Joined:
    Jan 9, 2013
    Messages:
    1
    Likes Received:
    0
    hey,can you send me the complete source for that repeating items in listview..
    thanks in advance..
     

Share This Page

Loading...