Random View Generator

You must Login before you can answer or comment on any questions.

I am currently have a table row that opens up to a bunch of views that are test questions - I would like it to open up randomly to a different view but when I open it now, no views are appearing

var allviews = [view1,view2,view3,view4,view5,view6,view7,view8,vertScrollView9,view10,view11,vertScrollView12,view13,view14,view15,vertScrollview16,vertScrollview17,vertScrollview18];
 
var scrollView = Titanium.UI.createScrollableView({
    views:allviews[Math.round(Math.random()*allviews)],
    showPagingControl:true,
    pagingControlHeight:25,
    maxZoomScale:2.0,
    currentPage:0
});
 
win.add(scrollView);
 
var i=1;
var activeView = allviews;
 
scrollView.addEventListener('scroll', function(e)
{
    activeView = e.view;  // the object handle to the view that is about to become visible
    i = e.currentPage;
    Titanium.API.info("scroll called - current index " + i + ' active view ' + activeView);
});
scrollView.addEventListener('click', function(e)
{
    Ti.API.info('ScrollView received click event, source = ' + e.source);
});
scrollView.addEventListener('touchend', function(e)
{
    Ti.API.info('ScrollView received touchend event, source = ' + e.source);
});

— asked 7 months ago by J W
0 Comments

1 Answer

I don't see a table row anywhere here, but it looks like the view you're adding to the scrollView wouldn't show correctly anyway. This here Math.round(Math.random()*allviews) is going to equal NaN. You would need to do something like this: Math.round(Math.random()*allviews.length) You'd may need to subtract 1 from the number or always round down because that could possibly return the length of the array which would be 1 more than the highest possible index.

Your Answer

Think you can help? Login to answer this question!