1

我需要创建一个需要 3 列并且可以有多行的表。我正在使用 BlackBerry API 版本 6。我已经调试了我的代码并且它给出了IllegalArgumentException. 我无法解决这个错误。

我的代码如下:

public class designTableLayout extends MainScreen{

    TableModel theModel = new TableModel();
    theView = new TableView(theModel);
    TableController theController = new TableController(theModel, theView,
                                                        TableController.FIELD_FOCUS);
    theView.setController(theController);

    HeaderTemplate theTemplate = new HeaderTemplate(theView, 1, 3);
    theTemplate.createRegion(new XYRect(0,0,1,1));
    theTemplate.createRegion(new XYRect(1,0,1,1));
    theTemplate.createRegion(new XYRect(2,0,1,1));
    theTemplate.setRowProperties(0, new TemplateRowProperties(60));
    theTemplate.setColumnProperties(0, new TemplateColumnProperties(40));
    theTemplate.setColumnProperties(1, new TemplateColumnProperties(40));
    theTemplate.setColumnProperties(2, new TemplateColumnProperties(40));
    theTemplate.useFixedHeight(true);
    theView.setDataTemplate(theTemplate);

    theModel.addRow(new String[]{"the","quick","brown"});// problem arises here
    theModel.addRow(new String[]{"jumps","over","the"});
    theModel.addRow(new String[]{"dog","the","quick"});
    add(theView);
}

class HeaderTemplate extends DataTemplate {
    LabelField field1 = new LabelField("field1");
    LabelField field2 = new LabelField("field2");
    LabelField field3 = new LabelField("field3");

    public HeaderTemplate(DataView view,int rows,int columns){
        super(view, rows, columns);
    }

    public Field[] getDataFields(int modelRowIndex) {
         TableModel theModel = (TableModel) getView().getModel();

         //Get the data for the row.
         Object[] data = {field1, field2, field3};
         data = (Object[]) theModel.getRow(modelRowIndex);

         //Create a array to hold all fields.
         Field[] theDataFields = new Field[data.length];
         theDataFields[0] = new LabelField(field1/*, DrawStyle.ELLIPSIS*/);
         theDataFields[1] = new LabelField(field2/*, DrawStyle.ELLIPSIS*/);
         theDataFields[2] = new LabelField(field3/*, DrawStyle.ELLIPSIS*/);

         return theDataFields;
    }

}
4

1 回答 1

1

我知道您可能正在使用其中一些代码来测试您的表格模型,但我认为您的模板应该看起来更像这样:

class HeaderTemplate extends DataTemplate {

    public HeaderTemplate(DataView view,int rows,int columns){
        super(view, rows, columns);
    }

    public Field[] getDataFields(int modelRowIndex) {
         TableModel theModel = (TableModel) getView().getModel();

         //Get the data for the row.
         Object[] data = (Object[]) theModel.getRow(modelRowIndex);

         //Create a array to hold all fields.
         Field[] theDataFields = new Field[data.length];
         theDataFields[0] = new LabelField((String)data[0], DrawStyle.ELLIPSIS);
         theDataFields[1] = new LabelField((String)data[1], DrawStyle.ELLIPSIS);
         theDataFields[2] = new LabelField((String)data[2], DrawStyle.ELLIPSIS);

         return theDataFields;
    }

}

然后将您的数据添加为Object[]

theModel.addRow(new Object[]{"the","quick","brown"});

这是关于此的黑莓示例

于 2012-11-19T16:06:12.587 回答