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,
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);
-
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
-
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. -
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. -
Are you calling setData(null) before this code? The stack trace has an error that is executed from setData.. (and a resulting NullPointerException)
-
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.
-
It looks like "data" might be null when you pass it to createTableView() can you double check it?
-
Sorry about that, I missed the part about KitchenSink :) I'll paste the code here and see if I can figure out the problem
-
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?