This is a migrated thread and some comments may be shown as answers.

Items Repeated until scrolled

2 Answers 57 Views
LoopingList
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Neil
Top achievements
Rank 1
Neil asked on 04 Apr 2011, 02:04 PM
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 Answers, 1 is accepted

Sort by
0
Accepted
Victor
Telerik team
answered on 04 Apr 2011, 03:21 PM
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>>
0
Neil
Top achievements
Rank 1
answered on 04 Apr 2011, 03:31 PM
Victor,

Thank you!  That did the trick.

Neil
Tags
LoopingList
Asked by
Neil
Top achievements
Rank 1
Answers by
Victor
Telerik team
Neil
Top achievements
Rank 1
Share this question
or