Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 100
Registered: ‎01-13-2011
My Device: Not Specified

Showing data into a table

[ Edited ]

Hello, I am trying to deploy a data into a table. And I used as reference this sample application:

 

http://docs.blackberry.com/en/developers/deliverables/18125/Table_Model_1328111_11.jsp

 

But I had to modificate some code because my data and row number is variable.

The problem is that my application is throwing this exception: java.lang.IllegalStateException: The # of fields returned by get DataFields should equal the # of regions in the template.

 

I do not know how to create the "Field" array and how to add regions to make them equal.

 

My code is attached:

 

class MyScreen extends MainScreen
{
	private RegionStyles _style;
	private TableModel _tableModel;
	private TableView _tableView;
	private TableController _controller;    

	private int NUM_ROWS;    
	private static final int NUM_COLUMNS = 4;
	
	private String[] column1Data;
	private Float[] column2Data;
	private String[] column3Data;
	private String[] column4Data;
	
	
	public MyScreen(int numRows, String[] column1Data, Float[] column2Data, String[] column3Data, String[] column4Data)
	{ 
		
		super(Manager.NO_VERTICAL_SCROLL);
		this.NUM_ROWS = numRows;
		this.column1Data = column1Data;
		this.column2Data = column2Data;
		this.column3Data = column3Data;
		this.column4Data = column4Data;
		setTitle("Data");

		_tableModel = new TableModel();

		_tableView = new TableView(_tableModel);
		_controller = new TableController(_tableModel, _tableView);
		_tableView.setController(_controller);
		
		setStyle();

		add(_tableView);

		for(int i = 0; i < NUM_ROWS; i++)
		{			
			_tableModel.addRow(new String[] {column1Data[i], column2Data[i].toString(), column3Data[i], column4Data[i]});
		}
		

	}

	public void setStyle()
	{
		DataTemplate dataTemplate = new DataTemplate(_tableView, NUM_ROWS, NUM_COLUMNS)
		{
			public Field[] getDataFields(int modelRowIndex)
			{
				Object[] data = (Object[]) _tableModel.getRow(modelRowIndex);
				System.out.println(modelRowIndex);
				Field[] fields = new Field[data.length];
				for(int i = 0; i < data.length; i++)
				{
					fields[i] = new LabelField(data[i]);
				}
				return fields;
			}
		};
		

		for(int i = 0; i < NUM_ROWS; i++)
		{
			dataTemplate.createRegion(new XYRect(0, i, 1, 1), _style);
			dataTemplate.createRegion(new XYRect(1, i, 1, 1), _style);
			dataTemplate.createRegion(new XYRect(2, i, 1, 1), _style);
			dataTemplate.createRegion(new XYRect(3, i, 1, 1), _style);
			dataTemplate.setRowProperties(i, new TemplateRowProperties(60));
		}

		dataTemplate.setColumnProperties(0, new TemplateColumnProperties(100));
		dataTemplate.setColumnProperties(1, new TemplateColumnProperties(80));
		dataTemplate.setColumnProperties(2, new TemplateColumnProperties(80));
		dataTemplate.setColumnProperties(3, new TemplateColumnProperties(80));

		_tableView.setDataTemplate(dataTemplate);
		dataTemplate.useFixedHeight(true);
	}
}

 

Any help will be appreciate

Developer
Posts: 100
Registered: ‎01-13-2011
My Device: Not Specified

Re: Showing data into a table

My problem has been resolved, I was confusing rownum with number of data rows. I just needed to add a regio per colomn in my single row (row in model).
New Contributor
Posts: 2
Registered: ‎05-02-2011
My Device: dsf
My Carrier: sdf

Re: Showing data into a table

could u pls update your latest code. i m facing same exception.
New Contributor
Posts: 2
Registered: ‎08-03-2011
My Device: Bold 9780
My Carrier: O2

Re: Showing data into a table

The solution is to only add 1 row style, as the OP stated the confusion is that he was adding one region per row/column, in addition you only need to set the row proerpeties once, once the properties have been set any column added will use this template...

 

 

// Incorrect		
for(int i = 0; i < NUM_ROWS; i++)
{
	dataTemplate.createRegion(new XYRect(0, i, 1, 1), _style);
	dataTemplate.createRegion(new XYRect(1, i, 1, 1), _style);
	dataTemplate.createRegion(new XYRect(2, i, 1, 1), _style);
	dataTemplate.createRegion(new XYRect(3, i, 1, 1), _style);
	dataTemplate.setRowProperties(i, new TemplateRowProperties(60));
}

// Correct		
for(int i = 0; i < NUM_COLUMNS; i++)
{
	dataTemplate.createRegion(new XYRect(i, 0, 1, 1), _style);
}

dataTemplate.setRowProperties(0, new TemplateRowProperties(60));

 Spent two days trying to work this out.

 

Cheers

Stedders