How to create UI controls in for loop?

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

Hi Guys, I am developing an android application in titanium studio 2.0.2 with Android 2.2. Currently I am stucked in a situation where I have to add UI controls (button and lables) in a loop because the no. of controls is dynamic and it depends on a variable which is returned from remote server.

    To do this I have created a function createGUI(data) which creates a complete set of the required controls and calling that function in a for loop. After creation of first UI controls set I increment the top property of all controls for the next set. Now the problem I am having is that all controls are created with the same name so I can't refer them or get their values later because they are of the same name....I've tried to introduce variables in the control names but it gives syntax error. Following is my code to do this:-

for(var pos=0; pos < json.length; pos++)
        {
            createGUI(data[pos]);
            //Here the top variables will be incremented.
        }
 
Titanium.UI.backgroundColor='#ffffff';
 
    var win = Titanium.UI.createWindow({
        width: 320,
        height: 480,
        top: 0,
        left: 0,
        backgroundColor:'#ffffff'   
    });
 
    var lbl_Auctions=Titanium.UI.createLabel({
        text: 'Live Auctions',
        backgroundColor: '#306327',
        color: '#ffffff',
        clickable: false,
        width: Titanium.UI.SIZE,
        height:Titanium.UI.SIZE,
        top: '3%' ,
        left: '2%',
        font: {fontSize:'25px',fontWeight: 'bold'}
    });
 
function createGUI(data)
{
    var btn_auc1=Titanium.UI.createButton({     //button for auction image
        //title: 'Login',
        width:'50%',
        height: '30%',
        top: '15%',
        left:'2%',
        font: {fontSize:'20px' ,fontWeight: 'bold'} 
    });
 
    //alert(data[2].id);
    var lbl_auc1Name=Titanium.UI.createLabel({
        text: data.name,
        clickable: true,
        width: Titanium.UI.SIZE,
        height:Titanium.UI.SIZE,
        top: '47%' ,
        left: '7%',
        font: {fontSize:'18px'} 
    });
 
    var lbl_auc1Price=Titanium.UI.createLabel({
        text: data.current_auction_price,
        width: Titanium.UI.SIZE,
        height:Titanium.UI.SIZE,
        top: '16%' ,
        left: '58%',
        font: {fontSize:'17px'} 
    });
 
    var lbl_auc1LB=Titanium.UI.createLabel({
        text: data.last_bidder,
        width: Titanium.UI.SIZE,
        height:Titanium.UI.SIZE,
        top: '27%' ,
        left: '58%',
        font: {fontSize:'17px'} 
    });
 
    var btn_Bid=Titanium.UI.createButton({
        title: 'Bid',
        backgroundColor: '#00ff00',
        width:Titanium.UI.SIZE,
        height: Titanium.UI.SIZE,
        top: '36%',
        left:'68%',
        font: {fontSize:'20px' ,fontWeight: 'bold'} 
    });
 
    win.add(lbl_Auctions);
    win.add(btn_auc1);
    win.add(lbl_auc1Name);
    win.add(lbl_auc1Price);
    win.add(lbl_auc1LB);
    win.add(btn_Bid);
}
win.open();
I know this is very pathetic way to do this but I am very new to Titanium and javascript so I've no other idea (use of views or other methods) to solve this problem, so pardon me if I make you laugh. Any help,suggestion or idea in this regard will be greatly appreciated and please reply ASAP as I am stucked here and can't do anything further. Many thanks in advance.

2 Answers

Accepted Answer

var controls = [];
var countControls = 5; // remote
for(var i = 0; i < countControls; i++) {
var control ={};
control.labelName = createNameLabel(i);
control.buttonLogin = createLoginButton(i);
...
addControl(control);
controls.push(control);
}
this should give you an idea how you could solve it and Keep your Code clean, you dont have to pay for functions :)

— answered 11 months ago by Alexander Bauer
answer permalink
8 Comments
  • I didn't get the code...can you please explain it???

    — commented 11 months ago by Muhammad Qasim Khan

  • You have an array (controls) which consists of objects (object per loop) each object has its own properties which you can use at later time to access any control. You could access them then through:

    var lblName = controls[idx].labelName;
    lblName.setText(...);
    Functions like createNameLabel etc. you will have to create, this is just the basic idea.

    — commented 11 months ago by Alexander Bauer

  • Ok...I am doin in this way:-

    var controls=[];
    for(var i;i<5;i++)
    {
    var control={};
        controls.btn1=Titanium.UI.createButton({        
            width:'50%',
            height: '30%',
            top: top1,
            left:'4%',
            font: {fontSize:'20px' ,fontWeight: 'bold'} 
        });
     
        controls.lbl1=Ti.UI.createLable({
     
        . . .   //add other components to control array
     
        controls.push(control);
    }
    view.add(controls);
    . . .   //other code
    But I get a Force Close error when I run the application. What is wrong?

    — commented 11 months ago by Muhammad Qasim Khan

  • Show 5 more comments

Hello , try in this way :

llbl_Auctions = [];
topprop = 55; // this is space between two labels one below the other
 
        for ( i = 0; i < 10; i++) { // number of time you want label to create set value accordingly
            llbl_Auctions[i] = Titanium.UI.createLabel({
                text : data[i],
                font: {
                    fontSize: 14,
                },
                top: 15+ i * topprop,
                width:110,
                left:10,
                height:40,
                textAlign:"left",
                color: 'White',
            });
 
            Window.add(llbl_Auctions[i]);
 
        }

— answered 11 months ago by Moiz Chhatriwala
answer permalink
8 Comments
  • Hi, Thanks for the quick reply. Good idea but the problem is that I don't have a single label. On set comprise of 2 buttons and 3 labels. How do I place them in array OR should I use a separate array for each control???

    — commented 11 months ago by Muhammad Qasim Khan

  • You can put 2 buttons and 3 labels also in the same loop , but in same way i suggest above , by making array .

    — commented 11 months ago by Moiz Chhatriwala

  • Use separate array for each control.

    — commented 11 months ago by Moiz Chhatriwala

  • Show 5 more comments

Your Answer

Think you can help? Login to answer this question!