Overlapping/Stacking of data in tableview object
Hello folks,
I'm pretty new to titanium developer and had recently found an interesting blog post at [http://mobile.tutsplus.com/tutorials/appcelerator/appcelerator-using-json-to-build-a-twitter-client/] demonstrating the use of the twitter API and displaying tweets fetched by the ti.network.httpclient in the ti.ui.tableview object. The example function (loadTweets()) written in the post was pretty straightforward so I modified it a little to accept a keyword argument. I also added a search box, a button and a button click event listener. I built and ran the sample app on my android phone.
The results appeared fine when the first search was fired. However, when I did a second search, the second set of results were stacked on/overlapping the first set of results.
I'm not sure what went wrong. I guess I probably need to 'flush' the tableview of the first search instance. But how do I do it? Any help is greatly appreciated!
Here's the code I'm using :
var win = Titanium.UI.currentWindow;
var b1 = Titanium.UI.createButton({
title:'Search',
height:35,
width:80,
top:10,
right:5
});
win.add(b1);
var tf1 = Titanium.UI.createTextField({
value:'techcrunch',
height:35,
top:10,
left:5,
width:200,
font:{fontSize:15},
borderStyle:Titanium.UI.INPUT_BORDERSTYLE_LINE
});
win.add(tf1);
b1.addEventListener('click', function()
{
if (tf1.value != '') {
loadTweets(tf1.value);
}else{
loadTweets("techcrunch");
}
});
function loadTweets(keyword)
{
var rowData = [];
var loader = Titanium.Network.createHTTPClient();
loader.open("GET","http://search.twitter.com/search.json?q="+keyword);
loader.onload = function()
{
var tweets = eval('('+this.responseText+')');
for (var i = 0; i < tweets.results.length; i++)
{
var tweet = tweets.results[i].text; // The tweet message
// var user = tweets.results[i].from_user; // The screen name of the user
var avatar = tweets.results[i].profile_image_url; // The profile image
var row = Titanium.UI.createTableViewRow({
height:'auto',
leftImage:avatar,
title:tweet
});
rowData[i] = row;
}
var tableView = Titanium.UI.createTableView({
data:rowData,
top:50,
left:0
});
win.add(tableView);
};
// Send the HTTP request
loader.send();
}
1 Answer
-
Accepted Answer
Your loadTweets function creates a new tableview and then adds this to the window every time you press the button.
What you should be doing is creating the tableview outside of this function and then the function should append rows to the tableview.