将TableModel.addRow中的数据插入Blackberry TableView

时间:2012-11-17 11:45:02

标签: blackberry java-me tableview blackberry-eclipse-plugin tablemodel

我需要创建一个表,其中需要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;
    }

}

1 个答案:

答案 0 :(得分: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"});

Here is the BlackBerry example on this