| Data reader class that is setup to consume data from an array data source. This class sets up the
field metadata for the data present in the array data source.
Object[][] data = new Object[][]{
new Object[]{"3m Co", new Double(71.72), new Double(0.02), new Double(0.03), "9/1 12:00am", 1},
new Object[]{"Alcoa Inc", new Double(29.01), new Double(0.42), new Double(1.47), "9/1 12:00am", 2}};
ArrayReader reader = new ArrayReader(new RecordDef(
new FieldDef[]{
new StringFieldDef("company"),
new FloatFieldDef("price"),
new FloatFieldDef("change"),
new FloatFieldDef("pctChange"),
new DateFieldDef("lastChanged", "n/j h:ia")
}
));
If the data array has data that doesn't exactly match the the fields you need in the Reader, then you can specify a
(zero based) positional mapper in the field definitions.
ArrayReader reader = new ArrayReader(new RecordDef(
new FieldDef[]{
new StringFieldDef("company"), //equivalent to new StringField("company", 0);
new FloatFieldDef("change", 2),
new DateFieldDef("lastChanged", "n/j h:ia", 4)
}
));
MemoryProxy dataProxy = new MemoryProxy(data);
Store store = new Store(dataProxy, reader);
store.load();
Sometimes you might want to map a field in the data source to be the "ID" of the record. You indicate the position
of the data element that represents the ID by using constructor that takes the field ID index as the first argument.
ArrayReader reader = new ArrayReader(5, new RecordDef(
new FieldDef[]{
new StringFieldDef("company"), //equivalent to new StringFieldDef("company", 0);
new FloatFieldDef("change", 2),
new DateFieldDef("lastChanged", "n/j h:ia", 4)
}
));
So for the above data set the ID for the first record will get mapped to 1 (data index of 5 corresponts to the last
element in the array) and the ID of the second row gets mapped to 2. Mapping ID's for Record's are useful when, say,
the data soource is mapped to a widget like a Grid and when the user clicks on a row, you want to geta handle of the
ID for that record which might correspond to the primary key of the database table from which the data is being read.
author: Sanjiv Jivan See Also: RecordDef since: 0.9 |