Looping through XML
I am building a 3 level deep table group. It is going to be populated through XML which I am loading with the method below. The issue is starting at the second level.
The first level shows up fine, 4 rows. Clicking on any of them results in displaying only 1 row and that 1 row is the very last item in the XML list. Can anyone see a problem here?
If you need to see the xml, I've pasted it here:
http://pastie.org/1219135
function loadXML(url)
{
Ti.API.info('>>>> loading RSS feed '+url);
xhr = Titanium.Network.createHTTPClient();
xhr.open('GET',url);
xhr.onload = function()
{
Ti.API.info('>>> got the feed! ... ');
var xml = this.responseXML;
// Find the channel element
var category = xml.documentElement.getElementsByTagName("category");
for (var i = 0; i < category.length; i++)
{
var categoryTitle = category.item(i).getAttribute("title");
var row = Ti.UI.createTableViewRow({
hasChild:true
});
var label = Ti.UI.createLabel({
left:9,
text: categoryTitle
});
row.add(label);
main_menu.appendRow(row);
var course = category.item(i).getElementsByTagName("course");
for (var j = 0; j < course.length; j++)
{
var courseTitle = course.item(j).getAttribute("title");
Ti.API.info(courseTitle);
var subWin = Ti.UI.createWindow({
title:courseTitle
});
var subTable = Ti.UI.createTableView({
style:Titanium.UI.iPhone.TableViewStyle.GROUPED
});
var subRow = Ti.UI.createTableViewRow();
var subLabel = Ti.UI.createLabel({
left:9,
text:courseTitle
});
subRow.add(subLabel);
subTable.appendRow(subRow);
subWin.add(subTable);
}
row.addEventListener('click',function(e)
{
mainTab.open(subWin);
});
}
};
xhr.send();
}
loadXML(url);
3 Answers
-
I've figured it out! Successfully load XML and make a 3 level deep menu. I hope others will find this useful.
The XML: http://pastie.org/1219135
main.js:
var win = Titanium.UI.currentWindow; var url = "http://localhost:8888/lineup.xml"; var tabGroup = Titanium.UI.createTabGroup(); //--> create base UI tab and root window var mainWin = Titanium.UI.createWindow({ title:'Line-Up Training', backgroundColor:'#fff', tabBarHidden:true }); var mainTab = Titanium.UI.createTab({ title:'Line-Up Training', window:mainWin }); var main_menu = Ti.UI.createTableView({ style:Titanium.UI.iPhone.TableViewStyle.GROUPED }); mainWin.add(main_menu); //--> add tabs tabGroup.addTab(mainTab); //--> open tab group tabGroup.open(); function addEventToRow(theRow,theWin) { theRow.addEventListener('click',function(e) { mainTab.open(theWin); }); }; function loadXML(url) { Ti.API.info('>>>> loading RSS feed '+url); xhr = Titanium.Network.createHTTPClient(); xhr.open('GET',url); xhr.onload = function() { Ti.API.info('>>> got the feed! ... '); var xml = this.responseXML; //--> Find the channel element //--> Start root menu var category = xml.documentElement.getElementsByTagName("category"); for (var i = 0; i < category.length; i++) { var categoryTitle = category.item(i).getAttribute("title"); var row = Ti.UI.createTableViewRow({ hasChild:true }); var label = Ti.UI.createLabel({ left:9, text: categoryTitle }); row.add(label); main_menu.appendRow(row); //--> End Root Menu //--> Start 2nd menu var course = category.item(i).getElementsByTagName("course"); var subWin = Ti.UI.createWindow({ title:categoryTitle }); var subTable = Ti.UI.createTableView({ style:Titanium.UI.iPhone.TableViewStyle.GROUPED }); for (var j = 0; j < course.length; j++) { var courseTitle = course.item(j).getAttribute("title"); var subRow = Ti.UI.createTableViewRow({ hasChild:true }); var subLabel = Ti.UI.createLabel({ left:9, text:courseTitle }); subRow.add(subLabel); subTable.appendRow(subRow); subWin.add(subTable); //--> End 2nd menu //--> Start 3rd menu var topic = course.item(j).getElementsByTagName("topic"); var subSubWin = Ti.UI.createWindow({ title:courseTitle }); var subSubTable = Ti.UI.createTableView({ style:Titanium.UI.iPhone.TableViewStyle.GROUPED }); for (var k = 0; k < topic.length; k++) { var topicTitle = topic.item(k).getAttribute("title"); var subSubRow = Ti.UI.createTableViewRow(); var subSubLabel = Ti.UI.createLabel({ left:9, text:topicTitle }); subSubRow.add(subSubLabel); subSubTable.appendRow(subSubRow); subSubWin.add(subSubTable); } //--> End 3rd menu //--> Add 2nd menu click event addEventToRow(subRow,subSubWin); } //--> Add root menu click event addEventToRow(row,subWin); } }; xhr.send(); } loadXML(url);
-
Thumbs up for solving it yourself and posting to help others! :)
-
Can anyone offer any insight into why ONLY the last item is showing up?