What is the correct way to create a report using a custom object list?
I’ve been trying to achieve this for a day or two, but nothing works.
I created a custom JRDataSource and a factory that creates such object.
Here’s the code for the DataSource:
public class ViajeDataSource implements JRDataSource
{
private int index;
private ArrayList<Viaje> viajes;
public ViajeDataSource()
{
this.viajes=new ArrayList<>();
}
@Override
public boolean next() throws JRException
{
this.index++;
return (index<this.viajes.size());
}
@Override
public Object getFieldValue(JRField jrf) throws JRException
{
Object value = null;
Viaje viaje=this.viajes.get(this.index);
if (jrf.getName().equals("FECHA"))
{
return viaje.getFecha();
}
switch(jrf.getName())
{
case "FECHA":
value=viaje.getFecha();
break;
case "NombrePersona":
value=viaje.getNombrePersona();
break;
case "NombreEmpresa":
value=viaje.getNombreEmpresa();
break;
case "Observacion":
value=viaje.getObservacion();
break;
case "Importe":
value=viaje.getTarifa();
break;
case "Demora":
value=viaje.getDemora();
break;
case "Total":
value=viaje.getTotal();
break;
case "Peaje":
value=viaje.getPeaje();
break;
}
return value;
}
Pay attention to the field named FECHA
I added the following expression in IReport designer: $F{FECHA}, but when previewing, the IDE says no “FECHA” field was found. Any ideas why?
This approach worked for me:
Instead of a custom datasource, I created a custom dataSourceProvider, and selected JRDataSource when creating the datasource.
This, combined to IReports standalone utility, allowed me to design the report with sample data from my collection