Items Repeated until scrolled

3 posts, 1 answers
  1. Neil
    Neil avatar
    9 posts
    Member since:
    Aug 2010

    Posted 04 Apr 2011 Link to this post

    I am doing something stupid with the loopinglist (I suspect) but I cannot see how to fix it.

    I have code to populate the list with minute values.  However, when I run it, the three items that are visible are repeated until I scroll them off the page.  From that point on it all works fine.  How do I initialise that initial view?

    Thanks in advance, here is the code:

    List<MinSec> minSecs = new List<MinSec>();
    minSecs.Add(new MinSec() { Description = "00", Value = 0 });
    minSecs.Add(new MinSec() { Description = "01", Value = 1 });
    minSecs.Add(new MinSec() { Description = "02", Value = 2 });
    minSecs.Add(new MinSec() { Description = "03", Value = 3 });
    minSecs.Add(new MinSec() { Description = "04", Value = 4 });
    minSecs.Add(new MinSec() { Description = "05", Value = 5 });
    minSecs.Add(new MinSec() { Description = "06", Value = 6 });
    minSecs.Add(new MinSec() { Description = "07", Value = 7 });
    minSecs.Add(new MinSec() { Description = "08", Value = 8 });
    minSecs.Add(new MinSec() { Description = "09", Value = 9 });
    minSecs.Add(new MinSec() { Description = "10", Value = 10 });
    minSecs.Add(new MinSec() { Description = "11", Value = 11 });
    minSecs.Add(new MinSec() { Description = "12", Value = 12 });
    minSecs.Add(new MinSec() { Description = "13", Value = 13 });
    minSecs.Add(new MinSec() { Description = "14", Value = 14 });
    minSecs.Add(new MinSec() { Description = "15", Value = 15 });
    minSecs.Add(new MinSec() { Description = "16", Value = 16 });
    minSecs.Add(new MinSec() { Description = "17", Value = 17 });
    minSecs.Add(new MinSec() { Description = "18", Value = 18 });
    minSecs.Add(new MinSec() { Description = "19", Value = 19 });
    minSecs.Add(new MinSec() { Description = "20", Value = 20 });
    minSecs.Add(new MinSec() { Description = "20", Value = 20 });
    minSecs.Add(new MinSec() { Description = "21", Value = 21 });
    minSecs.Add(new MinSec() { Description = "22", Value = 22 });
    minSecs.Add(new MinSec() { Description = "23", Value = 23 });
    minSecs.Add(new MinSec() { Description = "24", Value = 24 });
    minSecs.Add(new MinSec() { Description = "25", Value = 25 });
    minSecs.Add(new MinSec() { Description = "26", Value = 26 });
    minSecs.Add(new MinSec() { Description = "27", Value = 27 });
    minSecs.Add(new MinSec() { Description = "28", Value = 28 });
    minSecs.Add(new MinSec() { Description = "29", Value = 29 });
    minSecs.Add(new MinSec() { Description = "30", Value = 30 });
    LoopingListDataSource loopingListDataSource = new LoopingListDataSource(minSecs.Count);
    IEnumerator minSecEnumerator = minSecs.GetEnumerator();
    loopingListDataSource.ItemNeeded += (sender, args) =>
    {
        foreach (var minSec in minSecs)
        {
            LoopingListDataItem dataItem = new LoopingListDataItem();
            dataItem.Text = minSec.Description;
            args.Item = dataItem;
        }
    };
    loopingListDataSource.ItemUpdated += (sender, args) =>
    {
        args.Item.Text = minSecs[args.Index].Description;
    };
    this.radLoopingList1.DataSource = loopingListDataSource;
    this.radLoopingList1.SelectedIndex = 1;
    this.radLoopingList1.IsExpanded = true;
  2. Answer
    Victor
    Admin
    Victor avatar
    1351 posts

    Posted 04 Apr 2011 Link to this post

    Hi Neil,

     Thank you for writing.

    This happens because you are always iterating over your whole data source in the item needed event and
    you always overwrite the args.Item property with the last item in your minSecs collection. If you have a look at the online help, you will notice that for every ItemNeeded event invocation, the MoveNext() method of the enumerator is called. This advances the enumerator just enough in order to get only the currently visible items and the args.Items property is set to a new data item instance that corresponds to the real data. the ItemNeeded event is fired until all data items created. After this, only the ItemUpdated event fires, since all data items exist and simply need to be updated. 

    That said, the code in your ItemNeeded event should look like this:

    loopingListDataSource.ItemNeeded += (sender, args) =>
    {
        if (minSecEnumerator.MoveNext())
        {
            LoopingListDataItem dataItem = new LoopingListDataItem();
            dataItem.Text = (minSecEnumerator.Current as MinSec).Description;
            args.Item = dataItem;
        }
    };

    Please write again if you need further assistance with our controls.

    Kind regards,
    Victor
    the Telerik team
    Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
  3. DevCraft banner
  4. Neil
    Neil avatar
    9 posts
    Member since:
    Aug 2010

    Posted 04 Apr 2011 Link to this post

    Victor,

    Thank you!  That did the trick.

    Neil
Back to Top