Titanium Community Questions & Answer Archive

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

TableView getData problem

Hi,
I'am developing for Android and I have a problem with getData in TableView. When I'm using, for example:

var temp = tableview.getData()

it throws a Runtime Exception http://pastie.org/909154. When I try:

var temp = tableview.data

it returns temp as a function and I can not manipulate with the data. On the other hand, the setter works well. Does anyone have the same problem?

Regards,

— asked April 8th 2010 by Anh Tuan Kieu
  • android
  • getdata
  • tableview
0 Comments

9 Answers

  • I have checked the code, here is the "buggy" version of table_view_basic.js in KitchenSink:

    // create table view data object
    var data = [
        {title:'Row 1', hasChild:true, color:'red', selectedColor:'#fff'},
        {title:'Row 2', hasDetail:true, color:'green', selectedColor:'#fff'},
        {title:'Row 3', hasCheck:true, color:'blue', selectedColor:'#fff'},
        {title:'Row 4', color:'orange', selectedColor:'#fff'}
    
    
    ];
    
    // create table view
    var tableview = Titanium.UI.createTableView({
        data:data
    });
    
    var temp = tableview.getData(); //=> just for test, this line throws a Runtime Error
    // create table view event listener
    tableview.addEventListener('click', function(e)
    {
        // event data
        var index = e.index;
        var section = e.section;
        var row = e.row;
        var rowdata = e.rowData;
        Titanium.UI.createAlertDialog({title:'Table View',message:'row ' + row + ' index ' + index + ' section ' + section  + ' row data ' + rowdata}).show();
    });
    
    // add table view to the window
    Titanium.UI.currentWindow.add(tableview);
    
    — answered April 8th 2010 by Anh Tuan Kieu
    permalink
    0 Comments
  • Hi,

    I don't see a getData() method on the TableView object which will be why you get an exception. You are on the right track with the data property though. In the same way that you can use it to add an array of rows, its also an array you can inspect. Hope this snippit helps.

    // Here is a simple TableView

    var tView = Titanium.UI.createTableView({
    top:10,
    backgroundColor:'black'
    });

    // put some rows into it. I'm using tView.appendRow(…)

    // Now lets inspect that data

    Titanium.API.debug("tView length: " + tView.data.length);

    // Now lets get the first row:

    var aRow = tView.data[0]; // This is a TableViewRow object

    I hope that helps. Happy coding :-)

    Ian

    p.s. I'm using Titanium SDK 1.1.2 and 1.2.0

    — answered April 8th 2010 by Ian Buckner
    permalink
    2 Comments
    • The problem with this is that tView.data only stores the original table data, rather than the current data, meaning that as soon as you change the table data, tView.data is out of data.
      Note that my platform is android emulator and Ti 1.3.2

      — commented June 29th 2010 by Paul Dowsett
    • typo: "tView.data is out of data" s/b "tView.data is out of date"

      — commented June 29th 2010 by Paul Dowsett
  • Hi Ian,
    Thank you for your answer.
    Yes appendRow was working for me but when you want to add multiple rows at once, it's better to use setData as suggested by Don Thorp http://developer.appcelerator.com/question/4621/tableview—add-row-dynamically, that's why I want to have a variable to hold current data then add multiple rows to this array, after that I just have to call setData(data). For information, my app which uses getData() works well on 1.1.2. Apparently, there was a ticket for that https://appcelerator.lighthouseapp.com/projects/32238/tickets/334-android-missing-data-property-on-tableview and I see that it's fixed but that does not seem to be.

    — answered April 8th 2010 by Anh Tuan Kieu
    permalink
    0 Comments
  • Hi Ian,
    Thank you for your answer.
    Yes appendRow was working for me but when you want to add multiple rows at once, it's better to use setData as suggested by Don Thorp (http://developer.appcelerator.com/question/4621/tableview—add-row-dynamically), that's why I want to have a variable to hold current data then add multiple rows to this array, after that I just have to call setData(data). For information, my app which uses getData() works well on 1.1.2. Apparently, there was a ticket for that https://appcelerator.lighthouseapp.com/projects/32238/tickets/334-android-missing-data-property-on-tableview and I see that it's fixed but that does not seem to be.

    — answered April 8th 2010 by Anh Tuan Kieu
    permalink
    0 Comments
  • Are you calling setData(null) before this code? The stack trace has an error that is executed from setData.. (and a resulting NullPointerException)

    — answered April 8th 2010 by Marshall Culpepper
    permalink
    0 Comments
  • Hi Marshall,
    No, I don't call setData(null) before. You can stimulate by adding this line:

    var temp = tableview.getData(); //=> produces a runtime error
    

    after

    // create table view
    var tableview = Titanium.UI.createTableView({
        data:data
    });
    

    in table_view_basic.js in KitchenSink.

    — answered April 8th 2010 by Anh Tuan Kieu
    permalink
    0 Comments
  • It looks like "data" might be null when you pass it to createTableView() can you double check it?

    — answered April 8th 2010 by Marshall Culpepper
    permalink
    0 Comments
  • Sorry about that, I missed the part about KitchenSink :) I'll paste the code here and see if I can figure out the problem

    — answered April 8th 2010 by Marshall Culpepper
    permalink
    0 Comments
  • Marshall, I think I found the problem. I have to explicitly call tableview.setData(data) even if I've passed data as a parameter while initializing via:

    // create table view
    var tableview = Titanium.UI.createTableView({
        data:data
    });
    
    //var temp = tableview.getData(); //Doesn't work
    //I have to explicitly call
    tableview.setData(data);
    var temp = tableview.getData(); //Now it works
    

    I take a look at the source code: http://github.com/appcelerator/titanium_mobile/blob/master/android/modules/ui/src/ti/modules/titanium/ui/widget/TiUITableView.java, the data property is commented (line 135), might be that's the cause?

    — answered April 8th 2010 by Anh Tuan Kieu
    permalink
    1 Comment
    • This still seems to be a bug, as of 1.3.2.
      By the way, I really don't see the point of using setData(data) to get getData to work, when all you are getting is the information in the "data" array.
      I have raised a ticket here:
      #1215

      — commented June 29th 2010 by Paul Dowsett
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.