XML httpclients are mixed up

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

I have a view with 2 labels. Each has click event listeners which will open up new view on top of it and these 2nd views are all tableviews created by httpclients. httpclients works fine. It is communicating with server just fine and I'm also getting the data I want.

The problems is that when I add httpclient on second label, the 1st label's httpclient doesn't show up. And sometimes I see second label's tabelview under first label and vise-versa.

What could be the problem?

— asked 1 year ago by Nidhi Shah
2 Comments
  • Hi Nidhi

    are you trying to call the 2 httpclient at the same time? and as you have mentioned that the label is also dynamically generated by the httpclient code so if possible to share then share the code here. it might possible that the id to the tableview is not assigning properly.

    Regards

    Nikunj

    — commented 1 year ago by Nikunj S

  • Hi nikunj, I have two labels and each has saperate httpclient. So if first label is clicked, then only one httpclient is called. here's my first httpclient code:

    var firstData = [];
    var tableview = Ti.UI.createTableView({
        data: [],
        separatorColor: '#6fab70'
    });
    mainview.add(tableview);
    tableview.addEventListener('click',function(e){
        descrip.text = "Description: " +e.rowData.tagName1;
        headname.text = e.rowData.tagName2;
        funfact.text = "Fun Fact:" +e.rowData.tagName4;
        sciname.text = "Scientific Name: " +e.rowData.tagName3;
        view4.visible = true;   
    });
    var xhr = Ti.Network.createHTTPClient();
    xhr.onload = function(){
        if(this.readyState == 4){
            var doc = this.responseXML.documentElement;
     
            var items = doc.getElementsByTagName("item");
     
            for (var c=0;c<items.length;c++)
            {
                var item = items.item(c);
                var description = item.getElementsByTagName("description");
                var Scientific = item.getElementsByTagName("Scientific");
                var funfact = item.getElementsByTagName("FunFact");
     
                var thumbnails = item.getElementsByTagName("media:thumbnail");
                if (thumbnails && thumbnails.length > 0)
                {
                    var media = thumbnails.item(0).getAttribute("url");
                    var titlename = item.getElementsByTagName("title").item(0).text;
     
                    var row = Ti.UI.createTableViewRow({
                        height:80,
                        className: 'xmlData',   
     
                        tagName1 : item.getElementsByTagName("description").item(0).text,
                        tagName2 : item.getElementsByTagName("title").item(0).text,
                        tagName3 : item.getElementsByTagName("Scientific").item(0).text,
                        tagName4 : item.getElementsByTagName("FunFact").item(0).text,
     
                    });
                    var label = Ti.UI.createLabel({
                        text:titlename,
                        left:72,
                        top:5,
                        bottom:5,
                        right:5             
                    });
                    row.add(label);
                    var img = Ti.UI.createImageView({
                            image:media,
                            left:5,
                            height:60,
                            width:60
                    });
     
                    }
                    row.add(img);
                    firstData.push(row);    
                }
                tableview.setData(firstData); 
            }
    };  
    xhr.open("GET","http://www.myurl.com/file.xml");
    xhr.send();
    and this is the second one:
    var secondData = [];
    var tableview = Ti.UI.createTableView({
        data: [],
        separatorColor: '#6fab70'
    });
    mainview2.add(tableview);
    tableview.addEventListener('click',function(e){
        desc.text = "Description: " +e.rowData.tagName10;
        title.text = e.rowData.tagName20;
        funfact2.text = "Fun Fact: " +e.rowData.tagName40;
        scientific.text = "Scientific Name: " +e.rowData.tagName30;
        view5.visible = true;
     
    });
    var fauna = Ti.Network.createHTTPClient();
    fauna.onload = function(){
        if(this.readyState == 4){
            var doc = this.responseXML.documentElement;
     
            var items = doc.getElementsByTagName("item");
     
            for (var c=0;c<items.length;c++)
            {
                var item = items.item(c);
                var description = item.getElementsByTagName("Description");
                var Scientific = item.getElementsByTagName("Scientific");
                var funfact = item.getElementsByTagName("FunFact");
     
                var thumbnails = item.getElementsByTagName("media:thumbnail");
                if (thumbnails && thumbnails.length > 0)
                {
                    var media = thumbnails.item(0).getAttribute("url");
                    var titlename = item.getElementsByTagName("title").item(0).text;
     
                    var row2 = Ti.UI.createTableViewRow({
                        height:80,
                        className: 'xmlData',   
     
                        tagName10 : item.getElementsByTagName("Description").item(0).text,
                        tagName20 : item.getElementsByTagName("title").item(0).text,
                        tagName30 : item.getElementsByTagName("Scientific").item(0).text,
                        tagName40 : item.getElementsByTagName("FunFact").item(0).text,
     
                    });
                    var label = Ti.UI.createLabel({
                        text:titlename,
                        left:72,
                        top:5,
                        bottom:5,
                        right:5             
                    });
                    row2.add(label);
                    var img = Ti.UI.createImageView({
                            image:media,
                            left:5,
                            height:60,
                            width:60
                    });
     
                    }
                    row2.add(img);
                    secondData.push(row2);    
                }
                tableview.setData(secondData); 
            }
    };  
    fauna.open("GET","http://www.myurl.com/file2.xml");
    fauna.send();

    — commented 1 year ago by Nidhi Shah

2 Answers

Accepted Answer

Hi Nidhi,

When you saying " I see second label's tabelview under first label" that means you need to set zIndex values for both the tables.

— answered 1 year ago by Zarir Bhesania
answer permalink
3 Comments
  • Hi Zarir, As you suggested, I added zIndex to tableview. I added the same zIndex as the mainview. But now click event listerner isn't working.

    — commented 1 year ago by Nidhi Shah

  • Oh I figured. I made a lower zIndex of tabelviews than the mainview. And now it's not mixing up. Thanks Zarir !

    — commented 1 year ago by Nidhi Shah

  • Welcome

    — commented 1 year ago by Zarir Bhesania

there are many timing issues in Titanium. try using flags and timeouts

Your Answer

Think you can help? Login to answer this question!