I'm developing a mobile application (iOS, iPad & iPhone) and I'd like to randomise the views within a ScrollableView so that they're presented in a different order each time the user uses the app.
I'm new to Titanium, and not a "coder" by any means, so I might be going about this the wrong way. I was hoping I could get some help with the following code...
// CREATE 3 VIEWS var view1 = Ti.UI.createView({ backgroundImage:'1.png' }); var view2 = Ti.UI.createView({ backgroundImage:'2.png' }); var view3 = Ti.UI.createView({ backgroundImage:'3.png' }); // CREATE AN ARRAY USING THE ABOVE VIEWS var viewArray = ['view1','view2','view3']; // CREATE A "RANDOMISER" FUNCTION function Randomiser(){return 0.5-Math.random();}; // RANDOMISE THE ARRAY USING ABOVE FUNCTION viewArray.sort(Randomiser); // USE RANDOMISED ARRAY TO ADD VIEWS TO SCROLLABLE VIEW var scrollableView1 = Ti.UI.createScrollableView({ views: viewArray, });The code works, in that it takes the views and randomises them. However, I can't get it to spit out the result in a format that the ScrollableView will accept.
If i output the result to a text field I get results that read...
( view2, view1, view3)
so I'm assuming that when the same results are fed into the scrollableView, it must read as...
var scrollableView1 = Ti.UI.createScrollableView({ views: ( view2, view1, view3), });Any ideas on how to make this work?
7 Answers
Accepted Answer
Try this:
// CREATE 3 VIEWS var view1 = Ti.UI.createView({ backgroundImage:'1.png' }); var view2 = Ti.UI.createView({ backgroundImage:'2.png' }); var view3 = Ti.UI.createView({ backgroundImage:'3.png' }); // CREATE AN ARRAY USING THE ABOVE VIEWS var viewArray = [], tmpArray = [view1, view2, view3],//--- rand = Math.floor(Math.random() * 3); // RANDOMISE THE ARRAY viewArray.push(tmpArray[rand]); for(var i = 0; i < 3; i++) { if ( i != rand ) { viewArray.push(tmpArray[i]); } } // USE RANDOMISED ARRAY TO ADD VIEWS TO SCROLLABLE VIEW var scrollableView1 = Ti.UI.createScrollableView({ views: viewArray, });
If I write the code as you've suggested then the results come back as...
("[object TiUIView]", "[object TiUIView]", "[object TiUIView]")
views property of the scrollableview accepts only the array of the views so you should set as array like Nick said...
var scrollableView1 = Ti.UI.createScrollableView({ views: [view2, view1, view3], });you can get more information of scrollableview from here
I'm not disputing the way the scrollableView accepts it's views property. I'm just trying to achieve the end result of randomising those views. Obviously my method isn't working, which is why I'm asking for advice on how to achieve the desired effect.
then try this one....only for testing purpose and when you successfully run this code replace backgroundColor with backgroundImage
var view1 = Ti.UI.createView({ backgroundColor:'red' }); var view2 = Ti.UI.createView({ backgroundColor:'green' }); var view3 = Ti.UI.createView({ backgroundColor:'blue' }); // CREATE AN ARRAY USING THE ABOVE VIEWS var viewArray = [view1,view2,view3]; // CREATE A "RANDOMISER" FUNCTION function Randomiser(){return 0.5-Math.random();}; // RANDOMISE THE ARRAY USING ABOVE FUNCTION viewArray.sort(Randomiser); // USE RANDOMISED ARRAY TO ADD VIEWS TO SCROLLABLE VIEW var scrollableView1 = Ti.UI.createScrollableView({ views: viewArray });
Just to be clear, I'mm 100% aware that the code needs to be written as [view2, view1, view3], but unfortunately the randomising code I've written outputs ( view2, view1, view3). So either I need a new method of randomising the array to get the correct results (any suggestions?) or I need to find some way of rewriting the existing result to change it to the correct format (but there isn't an obvious way to rewrite a string in titanium studio, is there?)
I've tried that. As I mentioned earlier, that code returns a result of ("[object TiUIView]", "[object TiUIView]", "[object TiUIView]") which is of even less use to me.
Your Answer
Think you can help? Login to answer this question!