Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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();
}
— asked October 7th 2010 by Clifford Tan
  • android
  • data
  • mobile
  • overlapping
  • tableview
0 Comments

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.

    — answered October 7th 2010 by Roger Chapman
    permalink
    1 Comment
    • Hi Roger,

      You have a point. Thanks for the tip. I got it working by creating the tableview object outside the function and using only tableview.setData within the function to change the contents of the rows and append them to the tableview each time there is a new query. Thanks again. ^_^

      — commented October 7th 2010 by Clifford Tan
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.