The code below produces this outout:
If you try this code, vaadin automatically knows how to sort on CARMAKE & TOPSPEED when you click on their headers.
PASSENGER & DRIVER are not sorted on a header click.
How can I do this?
Where does the Table say "I don't know how to sort something of type PERSON so I won't add a click listener to that header"?
I was able to extend the beamItemContainer & override the sort method. I then could add buttons to the view that sort the columns. But I want to be able to click the table headers!
I had thought I could add TableHeaderClickListeners but why doesn't vaadin already do it for me when I override the beanItemContainer's sort? It already does this for CARMAKE & TOPSPEED.
#Override
public void init(VaadinRequest request) {
BeanItemContainer<SportsCar> beans = new BeanItemContainer<SportsCar>(SportsCar.class);
Person tom = new Person("Tom");
Person dick = new Person("Dick");
Person harry = new Person("Harry");
beans.addBean(new SportsCar("ferrari",180.0,tom,dick));
beans.addBean(new SportsCar("Aston Martin",165.0,harry,null));
Table t = new Table();
t.setContainerDataSource(beans);
t.setSizeFull();
this.setContent(t);
}
public class SportsCar {
String carMake;
double topspeed;
Person driver;
Person Passenger;
public SportsCar(String carMake, double topspeed, Person driver, Person passenger) {
this.carMake = carMake;
this.topspeed = topspeed;
this.driver = driver;
this.Passenger = passenger;
}
/**
* Get the carMake.
*
* #return the carMake.
*/
public String getcarMake() {
return carMake;
}
/**
* Set the carMake.
*
* #param carMake
* the carMake.
*/
public void setcarMake(String carMake) {
this.carMake = carMake;
}
/**
* Get the topspeed.
*
* #return the topspeed.
*/
public double getTopspeed() {
return topspeed;
}
/**
* Set the topspeed.
*
* #param topspeed
* the topspeed.
*/
public void setTopspeed(double topspeed) {
this.topspeed = topspeed;
}
/**
* Get the driver.
*
* #return the driver.
*/
public Person getDriver() {
return driver;
}
/**
* Set the driver.
*
* #param driver
* the driver.
*/
public void setDriver(Person driver) {
this.driver = driver;
}
/**
* Get the passenger.
*
* #return the passenger.
*/
public Person getPassenger() {
return Passenger;
}
/**
* Set the passenger.
*
* #param passenger
* the passenger.
*/
public void setPassenger(Person passenger) {
Passenger = passenger;
}
}
public class Person {
String carMake;
/**
* Create a Demo.Person.
*/
public Person(String carMake) {
this.carMake = carMake;
}
/**
* Get the carMake.
*
* #return the carMake.
*/
public String getcarMake() {
return carMake;
}
/**
* Set the carMake.
*
* #param carMake
* the carMake.
*/
public void setcarMake(String carMake) {
this.carMake = carMake;
}
#Override
public String toString(){
return carMake;
}
}
Java does not know how to compare objects per se. First you have to specify, that these objects can be compared by implementing the interface "Comparable". Then you need to override the compareTo method like so:
public int compareTo(Person p) {
return getcarMake().compareTo(p.getcarMake());
}
this should do the trick (I did not test it though).
Related
I have the method set and get:
public class ProdutosPedidoCompraBeans {
/**
* #return the odemNumero
*/
public String getOdemNumero() {
return odemNumero;
}
/**
* #param odemNumero the odemNumero to set
*/
public void setOdemNumero(String odemNumero) {
this.odemNumero = odemNumero;
}
/**
* #return the codEstoque
*/
public String getCodEstoque() {
return codEstoque;
}
/**
* #param codEstoque the codEstoque to set
*/
public void setCodEstoque(String codEstoque) {
this.codEstoque = codEstoque;
}
/**
* #return the quantidade
*/
public int getQuantidade() {
return quantidade;
}
/**
* #param quantidade the quantidade to set
*/
public void setQuantidade(int quantidade) {
this.quantidade = quantidade;
}
/**
* #return the unidade
*/
public String getUnidade() {
return unidade;
}
/**
* #param unidade the unidade to set
*/
public void setUnidade(String unidade) {
this.unidade = unidade;
}
/**
* #return the descricaoItem
*/
public String getDescricaoItem() {
return descricaoItem;
}
/**
* #param descricaoItem the descricaoItem to set
*/
public void setDescricaoItem(String descricaoItem) {
this.descricaoItem = descricaoItem;
}
/**
* #return the prazoEntrega
*/
public String getPrazoEntrega() {
return prazoEntrega;
}
/**
* #param prazoEntrega the prazoEntrega to set
*/
public void setPrazoEntrega(String prazoEntrega) {
this.prazoEntrega = prazoEntrega;
}
/**
* #return the precoUnitario
*/
public float getPrecoUnitario() {
return precoUnitario;
}
/**
* #param precoUnitario the precoUnitario to set
*/
public void setPrecoUnitario(float precoUnitario) {
this.precoUnitario = precoUnitario;
}
/**
* #return the valorDesconto
*/
public float getValorDesconto() {
return valorDesconto;
}
/**
* #param valorDesconto the valorDesconto to set
*/
public void setValorDesconto(float valorDesconto) {
this.valorDesconto = valorDesconto;
}
/**
* #return the icmsProduto
*/
public float getIcmsProduto() {
return icmsProduto;
}
/**
* #param icmsProduto the icmsProduto to set
*/
public void setIcmsProduto(float icmsProduto) {
this.icmsProduto = icmsProduto;
}
/**
* #return the ipiissProduto
*/
public float getIpiissProduto() {
return ipiissProduto;
}
/**
* #param ipiissProduto the ipiissProduto to set
*/
public void setIpiissProduto(float ipiissProduto) {
this.ipiissProduto = ipiissProduto;
}
/**
* #return the totalProdutoSemImpostos
*/
public float getTotalProdutoSemImpostos() {
return totalProdutoSemImpostos;
}
/**
* #param totalProdutoSemImpostos the totalProdutoSemImpostos to set
*/
public void setTotalProdutoSemImpostos(float totalProdutoSemImpostos) {
this.totalProdutoSemImpostos = totalProdutoSemImpostos;
}
/**
* #return the totalProdutoComImpostos
*/
public float getTotalProdutoComImpostos() {
return totalProdutoComImpostos;
}
/**
* #param totalProdutoComImpostos the totalProdutoComImpostos to set
*/
public void setTotalProdutoComImpostos(float totalProdutoComImpostos) {
this.totalProdutoComImpostos = totalProdutoComImpostos;
}
/**
* #return the valorIcmdoProduto
*/
public float getValorIcmdoProduto() {
return valorIcmdoProduto;
}
/**
* #param valorIcmdoProduto the valorIcmdoProduto to set
*/
public void setValorIcmdoProduto(float valorIcmdoProduto) {
this.valorIcmdoProduto = valorIcmdoProduto;
}
/**
* #return the valorIpiIssdoProduto
*/
public float getValorIpiIssdoProduto() {
return valorIpiIssdoProduto;
}
/**
* #param valorIpiIssdoProduto the valorIpiIssdoProduto to set
*/
public void setValorIpiIssdoProduto(float valorIpiIssdoProduto) {
this.valorIpiIssdoProduto = valorIpiIssdoProduto;
}
/**
* #return the pesquisaItens
*/
public String getPesquisaItens() {
return pesquisaItens;
}
/**
* #param pesquisaItens the pesquisaItens to set
*/
public void setPesquisaItens(String pesquisaItens) {
this.pesquisaItens = pesquisaItens;
}
}
my method DAO is:
public void salvarListaProdutos(ArrayList listaModelProdutos) {
this.conectaPedido.conecta();
try {
PreparedStatement pstmt = conectaPedido.conexao.prepareStatement("Insert into compras_itenspedidocompras(odemNumero,codEstoque,quantidade,unidade,descricaoItem,prazoEntrega,precoUnitario,valorDesconto,icmsProduto,ipiissProduto,totalProdutoSemImpostos,totalProdutoComImposto,valorIcmdoProduto,valorIpiIssdoProduto) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
for (int i = 0; i < listaModelProdutos.size(); i++) {
pstmt.setString(1, modelProdutos.getOdemNumero());
pstmt.setString(2, modelProdutos.getCodEstoque());
pstmt.setInt(3, modelProdutos.getQuantidade());
pstmt.setString(4, modelProdutos.getUnidade());
pstmt.setString(5, modelProdutos.getDescricaoItem());
pstmt.setString(6, modelProdutos.getPrazoEntrega());
pstmt.setFloat(7, modelProdutos.getPrecoUnitario());
pstmt.setFloat(8, modelProdutos.getValorDesconto());
pstmt.setFloat(9, modelProdutos.getIcmsProduto());
pstmt.setFloat(10, modelProdutos.getIpiissProduto());
pstmt.setFloat(11, modelProdutos.getTotalProdutoSemImpostos());
pstmt.setFloat(12, modelProdutos.getTotalProdutoComImpostos());
pstmt.setFloat(13, modelProdutos.getValorIcmdoProduto());
pstmt.setFloat(14, modelProdutos.getValorIpiIssdoProduto());
}
pstmt.execute();
JOptionPane.showMessageDialog(null,"Itens adicionados ao pedido com sucesso.","",JOptionPane.INFORMATION_MESSAGE);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null,"erro:"+e,"",JOptionPane.ERROR_MESSAGE);
} finally {
this.conectaPedido.desconecta();
}
}
action of button to save the list on MYSQL:
ArrayList itemTab = new ArrayList<>();
for(int e = 0; e
modProdutos.setOdemNumero(tabelProduto.getValueAt(e, 0).toString());
modProdutos.setCodEstoque(tabelProduto.getValueAt(e, 1).toString());
modProdutos.setQuantidade(Integer.parseInt(tabelProduto.getValueAt(e, 2).toString()));
modProdutos.setUnidade(tabelProduto.getValueAt(e, 3).toString());
modProdutos.setDescricaoItem(tabelProduto.getValueAt(e, 4).toString());
modProdutos.setPrazoEntrega(tabelProduto.getValueAt(e, 5).toString());
modProdutos.setPrecoUnitario(Float.parseFloat(tabelProduto.getValueAt(e, 6).toString()));
modProdutos.setValorDesconto(Float.parseFloat(tabelProduto.getValueAt(e, 7).toString()));
modProdutos.setIcmsProduto(Float.parseFloat(tabelProduto.getValueAt(e, 8).toString()));
modProdutos.setIpiissProduto(Float.parseFloat(tabelProduto.getValueAt(e, 9).toString()));
modProdutos.setTotalProdutoSemImpostos(Float.parseFloat(tabelProduto.getValueAt(e, 10).toString()));
modProdutos.setTotalProdutoComImpostos(Float.parseFloat(tabelProduto.getValueAt(e, 11).toString()));
modProdutos.setValorIcmdoProduto(Float.parseFloat(tabelProduto.getValueAt(e, 12).toString()));
modProdutos.setValorIpiIssdoProduto(Float.parseFloat(tabelProduto.getValueAt(e, 13).toString()));
itemTab.add(modProdutos);
}
control.salvarListaProdutos(itemTab);
but in the MYSQL Table, the values are as "0" the float fields and as empity Strings fields.
Where have I mistaken?
Here is my problem I can't seem to figure out how to invoke a ParkingTicket object if (carMinutesPaid>meterMinutesPaid)? can any one help here are the details below to the question.
public static ParkingTicket checkParking(int carMinutesParked, int meterMinutesPaid)
{
Car parker = carMinutesParked;
ParkingMeter parkee = parkee;
if(carMinutesParked>meterMinutesPaid){
return new ParkingTicket(parker, parkee);
}
else if(carMinutesParked<=meterMinutesPaid){
System.out.println("null");
}
return new ParkingTicket(parker, parkee);
}
Here is the question for my project.
Remember, this method must be able to be used without a ParkingTicket object in existence.
Using a Car parameter and a ParkingMeter parameter, decide whether a ParkingTicket object should be created.
Invoke ParkingTicket(parker, parkee) if a ticket was merited, and return the result.
Return null if a ticket was not merited.
Here is my car class:
/**
* This is a Car class for Impark.
*
* #author Tre
* #version 2.0 15 October 2015
*/
public class Car
{
private static final int MINIMUM_PLATE_LENGTH=2;
private static final int MAXIMUM_PLATE_LENGTH=7;
public static final char MANUAL_TRANSMISSION='m';
public static final char AUTOMATIC_TRANSMISSION='a';
private static int defaultMinutesParked = 0;
private static double defaultOdometerInKm = 50000.5;
private String licensePlate;
private char transmissionType;
private double odometerInKm;
private int minutesParked;
/**
* #param newProposedLicensePlate the license plate of the car can equal null
* but must be between MINIMUM_PLATE_LENGTH and MAXIMUM_PLATE_LENGTH
*/
public Car(String newProposedLicensePlate)
{
setLicensePlate(newProposedLicensePlate);
transmissionType = AUTOMATIC_TRANSMISSION;
odometerInKm = defaultOdometerInKm;
minutesParked = defaultMinutesParked;
}
/**
* #return the license plate of the car can equal null
* but must be between MINIMUM_PLATE_LENGTH and MAXIMUM_PLATE_LENGTH
*/
public String getLicensePlate()
{
return licensePlate;
}
/**
* #return the transmission type MANUAL_TRANSMISSION or AUTOMATIC_TRANSMISSION
*/
public char getTransmissionType()
{
return transmissionType;
}
/**
* #return the odometer in kilometers
*/
public double getOdometerInKm()
{
return odometerInKm;
}
/**
* Recieve the license plate
* Mutator.licensePlate.
* #param proposedLicense String Conforming to ICBC *length* guidlines:
* http://www.icbc.com/vehicle-registration/license-plates/Pages/Personalized-licence-plates.aspx
* May also be null. The null represents a car without a plate
* If validation fails, null will be set.
*/
public void setLicensePlate(String proposedLicense)
{
if(proposedLicense==null){
licensePlate = proposedLicense;
}
else if(proposedLicense.length()>=MINIMUM_PLATE_LENGTH && proposedLicense.length()<=MAXIMUM_PLATE_LENGTH){
licensePlate = proposedLicense;
}
else{
licensePlate = null;
}
}
/**
* #param mOrA recieve the transmission type MANUAL_TRANSMISSION or AUTOMATIC_TRANSMISSION
* if invalid type of transmission is entered then will return "Installation failure: 'mOrA' is not a vaild transmission type"
*/
public void setTransmissionType(char mOrA)
{
if(mOrA==MANUAL_TRANSMISSION){
transmissionType = mOrA;
}
else if(mOrA==AUTOMATIC_TRANSMISSION){
transmissionType = mOrA;
}
else if (mOrA==mOrA){
System.out.println("Installation failure:" + " " + ("'")+(mOrA)+("'") + " " + "is not a valid transmission type.");
}
else{
transmissionType = mOrA;
}
}
/**
* #return the value of the odometer in with the String kilometers
*/
public String readOdometer()
{
return odometerInKm + " " + "kilometers";
}
/**
* #return the false if the minutesParked equals zero; otherwise true
*/
public boolean isParked()
{
if(minutesParked==defaultMinutesParked){
return false;
}
else{
return true;
}
}
/**
* #param duration replaces any existing value in minutesParked with the value from duration
*/
public void park(int duration)
{
if(duration>=defaultMinutesParked){
minutesParked = duration;
}
}
/**
* #param aOdometerInKm recieve the odometer in kilometers
*/
public void setOdometerInKm(double aOdometerInKm)
{
odometerInKm = aOdometerInKm;
}
/**
* #param aMinutesParked recieve the minutes parked in the stall but can not be a negative number
* if invalid number of minutes is entered then the number of minutes will not change.
*/
public void setMinutesParked(int aMinutesParked)
{
if(aMinutesParked>=defaultMinutesParked){
minutesParked = aMinutesParked;
}
else{
return;
}
}
/**
* #return the minutes parked
*/
public int getMinutesParked()
{
return minutesParked;
}
}
here is my ParkingMeter class:
/**
* This is a ParkingMeter class for Impark.
*
* #author Tre
* #version 2.0 15 October 2015
*/
public class ParkingMeter
{
private int minutesPaid;
private String methodPaid;
/**
* #param newMinutesPaid the minutes paid for parking meter
*/
public ParkingMeter()
{
}
/**
* #return the minutes paid
*/
public int getMinutesPaid()
{
return minutesPaid;
}
/**
* #return the method paid
*/
public String getMethodPaid()
{
return methodPaid;
}
/**
* #param paidBy the payment method customer will paid by
*/
public void setMethodPaid(String paidBy) /* BONUS for creating method paid */
{
if(methodPaid=="Visa"){
methodPaid = paidBy;
}
else if(methodPaid=="Master Card"){
methodPaid = paidBy;
}
else if(methodPaid=="American Express"){
methodPaid = paidBy;
}
else if(methodPaid=="Cash"){
methodPaid = paidBy;
}
else if(methodPaid=="Debit"){
methodPaid = paidBy;
}
else{
methodPaid = paidBy;
}
}
/**
* #param quantity the added minutes paid must not have a negative number
*/
public void addMinutesPaid(int quantity)
{
if(quantity>=0){
minutesPaid+=quantity;
}
}
}
and here is my ParkingTicket class:
/**
* This is a ParkingTicket class for Impark.
*
* #author Tre
* #version 1.0
*/
public class ParkingTicket
{
private final String referenceNumber;
private static String carLicensePlate;
private static int carMinutesParked;
private static int meterMinutesPaid;
private static int count = 1000;
private static String PREFIX = "V";
/**
* #param recorededLicense the value of the tick number
*/
private ParkingTicket(String recordedLicense, int newCarMinutesParked, int newMeterPaidMinutes)
{
referenceNumber = (PREFIX+count++);
carMinutesParked = newCarMinutesParked;
meterMinutesPaid = newMeterPaidMinutes;
}
/**
* #param
*/
private ParkingTicket(Car parker, ParkingMeter parkee)
{
this(parker.getLicensePlate(), parker.getMinutesParked(), parkee.getMinutesPaid());
}
/**
* #return referenceNumber the reference number
*/
public String getReferenceNumber()
{
return referenceNumber;
}
/**
* #return carLicensePlate the car's license plate
*/
public String getCarLicensePlate()
{
return carLicensePlate;
}
/**
* #return carMinutesParked the minutes car was parked
*/
public int getCarMinutesParked()
{
return carMinutesParked;
}
/**
* #return meterMinutesPaid the minutes paid on meter
*/
public int getMeterMinutesPaid()
{
return meterMinutesPaid;
}
/**
* #return count the with initial value of 1000
*/
public int getCount()
{
return count;
}
public static ParkingTicket checkParking(int carMinutesParked, int meterMinutesPaid)
{
Car parker = carMinutesParked;
ParkingMeter parkee = parkee;
if(carMinutesParked>meterMinutesPaid){
return new ParkingTicket(parker, parkee);
}
else if(carMinutesParked<=meterMinutesPaid){
return null;
}
return new ParkingTicket(parker, parkee);
}
}
This requirement:
Using a Car parameter and a ParkingMeter parameter, decide whether a
ParkingTicket object should be created.
suggests that you provide two parameters to the checkParking method, one is of the type Car and one of the ParkingMeter. So it should be like so:
public static ParkingTicket checkParking(Car car, ParkingMeter meter)
This code :
Car parker = carMinutesParked;
ParkingMeter parkee = parkee;
won't even compile
line 1: you're trying to assign int to object - that's called type mismatch.
line 2: variable parkee is not declared anywhere (except for the headline of the question).
You see, only the Car object holds the information about the parking duration, and you need the object for creating parking ticket. Same for the ParkingMeter
It should be vice versa - you get the values from the objects:
int carMinutesParked = car.getMinutesParked();
int meterMinutesPaid = meter.getMinutesPaid();
and proceed from here with if( or even use it in if without declaring temporary variables).
This one:
Invoke ParkingTicket(parker, parkee) if a ticket was merited, and
return the result.
you did OK.
Now this requirement:
Return null if a ticket was not merited.
suggest the method will return null, not string that equals to "null" .
So, based on these requirements it should rather be:
public static ParkingTicket checkParking(Car car, ParkingMeter meter)
{
//sanity check (bonus)
if ((car == null) || (meter == null))
return null;
if(car.getMinutesParked() > meter.getMinutesPaid()){
return new ParkingTicket(car, meter);
}
return null;
}
Note however, I don't know if you need any additional logic in this code and don't suggest this should be your final version, just explaining the general approach.
I am getting this warning when using Gson to convert a JSON string to a Java object. Why am I getting it and how can I solve it?
This is the warning I getting in code:
Unchecked assignment:
'net.brawtasports.brawtasportsgps.model.JSONKeys' to
'net.brawtasports.brawtasportsgps.model.JSONKeys<net.brawtasports.brawtasportsgps.model.team.Object>'
This is the error I get at runtime:
java.lang.ClassCastException:
com.google.gson.internal.LinkedTreeMap cannot be cast to
net.brawtasports.brawtasportsgps.model.team.Object
And this is my code:
String jsonString = Preferences.readFromPreferences(ApplicationConstants.team_data,"");
Gson gson = new Gson();
JSONKeys<Object> teamMembers = gson.fromJson(jsonString, JSONKeys.class); //converts json string to java object
Object players = teamMembers.getObject();//object is my custom class
//ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_dropdown_item,players);
ArrayAdapter arrayAdapter = new ArrayAdapter(getActivity(),android.R.layout.simple_spinner_dropdown_item,players.getPlayersSummary());
player1.setAdapter(arrayAdapter);
Here is the code for my JSONKeys POJO:
public class JSONKeys<T> {
private boolean Success;
private String Message;
private int ObjectIdentifier;
private T Object;
private java.util.List<List> List = new ArrayList<List>();
private int TotalRecords;
private String ErrorMessage;
private int Code;
private net.brawtasports.brawtasportsgps.model.match.Criteria Criteria;
private net.brawtasports.brawtasportsgps.model.match.SearchInfo SearchInfo;
//Add Object from match class
/**
* #return The Criteria
*/
public net.brawtasports.brawtasportsgps.model.match.Criteria getCriteria() {
return Criteria;
}
/**
* #param Criteria The Criteria
*/
public void setCriteria(net.brawtasports.brawtasportsgps.model.match.Criteria Criteria) {
this.Criteria = Criteria;
}
/**
* #return The SearchInfo
*/
public net.brawtasports.brawtasportsgps.model.match.SearchInfo getSearchInfo() {
return SearchInfo;
}
/**
* #param SearchInfo The SearchInfo
*/
public void setSearchInfo(net.brawtasports.brawtasportsgps.model.match.SearchInfo SearchInfo) {
this.SearchInfo = SearchInfo;
}
/**
* #return The List
*/
public java.util.List<List> getList() {
return List;
}
/**
* #param List The List
*/
public void setLArrayListList(java.util.List<List> List) {
this.List = List;
}
/**
* #return The Code
*/
public int getCode() {
return Code;
}
/**
* #param Code The Code
*/
public void setCode(int Code) {
this.Code = Code;
}
/**
* #return The TotalRecords
*/
public int getTotalRecords() {
return TotalRecords;
}
/**
* #param TotalRecords The TotalRecords
*/
public void setTotalRecords(int TotalRecords) {
this.TotalRecords = TotalRecords;
}
/**
* #return The ErrorMessage
*/
public String getErrorMessage() {
return ErrorMessage;
}
/**
* #param ErrorMessage The ErrorMessage
*/
public void setErrorMessage(String ErrorMessage) {
this.ErrorMessage = ErrorMessage;
}
/**
*
* #return
* The message
*/
public String getMessage() {
return Message;
}
/**
*
* #param message
* The message
*/
public void setMessage(String message) {
this.Message = message;
}
/**
*
* #return
* The Success
*/
public boolean isSuccess() {
return Success;
}
/**
*
* #param Success
* The Success
*/
public void setSuccess(boolean Success) {
this.Success = Success;
}
/**
*
* #return
* The ObjectIdentifier
*/
public int getObjectIdentifier() {
return ObjectIdentifier;
}
/**
*
* #param ObjectIdentifier
* The ObjectIdentifier
*/
public void setObjectIdentifier(int ObjectIdentifier) {
this.ObjectIdentifier = ObjectIdentifier;
}
/**
*
* #return
* The Object
*/
public T getObject() {
return Object;
}
/**
*
* #param Object
* The Object
*/
public void setObject(T Object) {
this.Object = Object;
}
}
and for my Object POJO it is:
public class Object {
private HomeTeamGoals HomeTeamGoals;
private AwayTeamGoals AwayTeamGoals;
private List<PlayersSummary> PlayersSummary = new ArrayList<PlayersSummary>();
private TeamSummary TeamSummary;
/**
*
* #return
* The HomeTeamGoals
*/
public HomeTeamGoals getHomeTeamGoals() {
return HomeTeamGoals;
}
/**
*
* #param HomeTeamGoals
* The HomeTeamGoals
*/
public void setHomeTeamGoals(HomeTeamGoals HomeTeamGoals) {
this.HomeTeamGoals = HomeTeamGoals;
}
/**
*
* #return
* The AwayTeamGoals
*/
public AwayTeamGoals getAwayTeamGoals() {
return AwayTeamGoals;
}
/**
*
* #param AwayTeamGoals
* The AwayTeamGoals
*/
public void setAwayTeamGoals(AwayTeamGoals AwayTeamGoals) {
this.AwayTeamGoals = AwayTeamGoals;
}
/**
*
* #return
* The PlayersSummary
*/
public List<PlayersSummary> getPlayersSummary() {
return PlayersSummary;
}
/**
*
* #param PlayersSummary
* The PlayersSummary
*/
public void setPlayersSummary(List<PlayersSummary> PlayersSummary) {
this.PlayersSummary = PlayersSummary;
}
/**
*
* #return
* The TeamSummary
*/
public TeamSummary getTeamSummary() {
return TeamSummary;
}
/**
*
* #param TeamSummary
* The TeamSummary
*/
public void setTeamSummary(TeamSummary TeamSummary) {
this.TeamSummary = TeamSummary;
}
}
You need to use a fully specified type when deserializing generics. They key line that needs changing is here:
JSONKeys<Object> teamMembers = gson.fromJson(jsonString, JSONKeys.class);
Because you're using JSONKeys.class, Gson doesn't know what the generic type of JSONKeys is. Instead, use this:
JSONKeys<Object> teamMembers = gson.fromJson(jsonString,
new TypeToken<JSONKeys<net.brawtasports.brawtasportsgps.model.team.Object>>(){}.getType());
This will tell Gson which generic type you need to use. See my explanation for why this works here: How does Gson TypeToken work?
As an aside, don't name your classes the same thing as anything in the java.lang package. It makes your code really confusing for people reading it and can cause unforseen bugs when you accidentally refer to the wrong one by forgetting an import statement.
This table is produced by my code below. (The table's data is rubbish)
Question:
How can I get a list of the items under a certain column in the order they appear?
I want to do something like table.GetListFromColumn("carmake") so I would have ["Aston Martin" , "Porche 911", "ferrari"].
Or beanItemContainer.GetListFromColumn("carmake")?
#Override
public void init(VaadinRequest request) {
BrakeResponseTime time1 = new BrakeResponseTime(0.2f);
BrakeResponseTime time2 = new BrakeResponseTime(0.8f);
BrakeResponseTime time3 = new BrakeResponseTime(0.5f);
Collection<SportsCar>c = new ArrayList<SportsCar>();
c.add(new SportsCar("ferrari", 180.0, "Tom", time1));
c.add(new SportsCar("Aston Martin", 165.0, "Harry", time2));
c.add(new SportsCar("Porche 911", 145.0, "Dick", time3));
MyBeanItemContainer container = new MyBeanItemContainer(SportsCar.class,c);
Table table = new Table();
t.setContainerDataSource(container);
this.setContent(table);
}
.
public class BrakeResponseTime implements Comparable<BrakeResponseTime> {
float time;
/**
* Create a Demo.BrakeResponseTime.
*/
public BrakeResponseTime(float time) {
this.time = time;
}
/**
* Get the time.
*
* #return the time.
*/
public float getTime() {
return time;
}
/**
* Set the time.
*
* #param time
* the time.
*/
public void setTime(float time) {
this.time = time;
}
#Override
public String toString() {
return time +"/%";
}
/*
* (non-Javadoc)
*
* #see java.lang.Comparable#compareTo(java.lang.Object)
*/
#Override
public int compareTo(BrakeResponseTime o) {
if (null == o) {
return 1;
}
if(time==o.getTime()){
return 0;
}
return (time < o.getTime() ? -1 : 1);
}
}
.
public class MyBeanItemContainer extends BeanItemContainer<SportsCar> {
/**
* Create a Demo.MyBeanItemContainer.
*/
public MyBeanItemContainer(Class<SportsCar> type, Collection<SportsCar> collection) {
super(type, collection);
}
private int compareBrakeResponseTimeWithNullCheck(BrakeResponseTime o1, BrakeResponseTime o2) {
if (null == o1 && (null == o2)) {
return 0;
} else if (null == o1 && (null != o2)) {
return -1;
}else if(null != o1 && (null == o2)){
return 1;
} else {
return o1.compareTo(o2);
}
}
#Override
public void sort(final Object[] propertyId, final boolean[] ascending) {
super.sort(propertyId, ascending);
System.out.println(Arrays.toString(propertyId));
final boolean sortAscending = ascending[0];
final Object sortContainerPropertyId = propertyId[0];
List<SportsCar> list = super.getAllItemIds();
Collections.sort(list, new Comparator<SportsCar>() {
#Override
public int compare(final SportsCar o1, final SportsCar o2) {
int result = 0;
if ("responseTime".equals(sortContainerPropertyId)) {
result = compareBrakeResponseTimeWithNullCheck(o1.getResponseTime(), o2.getResponseTime());
}
if (!sortAscending) {
result *= -1;
}
return result;
}
});
}
}
public class SportsCar {
String carMake;
double topspeed;
String driver;
BrakeResponseTime responseTime;
public SportsCar(String carMake, double topspeed, String driver, BrakeResponseTime responseTime) {
this.carMake = carMake;
this.topspeed = topspeed;
this.driver = driver;
this.responseTime = responseTime;
}
/**
* Get the carMake.
*
* #return the carMake.
*/
public String getcarMake() {
return carMake;
}
/**
* Set the carMake.
*
* #param carMake
* the carMake.
*/
public void setcarMake(String carMake) {
this.carMake = carMake;
}
/**
* Get the topspeed.
*
* #return the topspeed.
*/
public double getTopspeed() {
return topspeed;
}
/**
* Set the topspeed.
*
* #param topspeed
* the topspeed.
*/
public void setTopspeed(double topspeed) {
this.topspeed = topspeed;
}
/**
* Get the driver.
*
* #return the driver.
*/
public String getDriver() {
return driver;
}
/**
* Set the driver.
*
* #param driver
* the driver.
*/
public void setDriver(String driver) {
this.driver = driver;
}
/**
* Get the responseTime.
* #return the responseTime.
*/
public BrakeResponseTime getResponseTime() {
return responseTime;
}
/**
* Set the responseTime.
* #param responseTime the responseTime.
*/
public void setResponseTime(BrakeResponseTime responseTime) {
this.responseTime = responseTime;
}
}
I hadn't realised I could access the bean container's properties in this way.
container.getIdByIndex(0).getCarMake()
This is what I was looking for!
I'm creating small javaEE Application I have problem with vendorsProdcut name dropdownbox is not showing name only array object like this see the images
Just want to ask how can I get the product name from arraylist to fix my problem?
just wondering is there are away to bring arraylist product name from
productViewModel.getAllProductForVendor
HTML
<div class="ui-block-a">
<h:selectOneMenu id="productname" value="#{productViewModel.prodname}" onchange="javascript: return this.form.submit();">
<f:selectItems value="#{productViewModel.getAllProductForVendor(vendorViewModel.vendorno)}"></f:selectItems>
</h:selectOneMenu>
</div>
Java Beans
public ArrayList<ProductEJBDTO> getAllProductForVendor(int vendorno) {
ArrayList<ProductEJBDTO> Products = new ArrayList() ;
//model.getProducts(vendorno);
try
{
Products = productFacadeBean.getAllProductsForVendor(vendorno);
}
catch(Exception e)
{
System.out.println("can't get products for vendors " + e);
}
return Products;
}
DTO
package case2dtos;
import java.io.Serializable;
import java.math.BigDecimal;
/**
*
* #author abdallaelnajjar
*/
public class ProductEJBDTO implements Serializable {
public ProductEJBDTO(){}
private int vendorno;
private String prodcd;
private String vensku;
private String prodnam;
private BigDecimal costprice;
private BigDecimal msrp;
private int rop;
private int eoq;
private int qoh;
private int qoo;
private byte[] qrcode;
/**
* #return the prodcd
*/
public String getProdcd() {
return prodcd;
}
/**
* #param prodcd the prodcd to set
*/
public void setProdcd(String prodcd) {
this.prodcd = prodcd;
}
/**
* #return the vensku
*/
public String getVensku() {
return vensku;
}
/**
* #param vensku the vensku to set
*/
public void setVensku(String vensku) {
this.vensku = vensku;
}
/**
* #return the prodnam
*/
public String getProdnam() {
return prodnam;
}
/**
* #param prodnam the prodnam to set
*/
public void setProdnam(String prodnam) {
this.prodnam = prodnam;
}
/**
* #return the costprice
*/
public BigDecimal getCostprice() {
return costprice;
}
/**
* #param costprice the costprice to set
*/
public void setCostprice(BigDecimal costprice) {
this.costprice = costprice;
}
/**
* #return the msrp
*/
public BigDecimal getMsrp() {
return msrp;
}
/**
* #param msrp the msrp to set
*/
public void setMsrp(BigDecimal msrp) {
this.msrp = msrp;
}
/**
* #return the rop
*/
public int getRop() {
return rop;
}
/**
* #param rop the rop to set
*/
public void setRop(int rop) {
this.rop = rop;
}
/**
* #return the eoq
*/
public int getEoq() {
return eoq;
}
/**
* #param eoq the eoq to set
*/
public void setEoq(int eoq) {
this.eoq = eoq;
}
/**
* #return the qoh
*/
public int getQoh() {
return qoh;
}
/**
* #param qoh the qoh to set
*/
public void setQoh(int qoh) {
this.qoh = qoh;
}
/**
* #return the qoo
*/
public int getQoo() {
return qoo;
}
/**
* #param qoo the qoo to set
*/
public void setQoo(int qoo) {
this.qoo = qoo;
}
/**
* #return the qrcode
*/
public byte[] getQrcode() {
return qrcode;
}
/**
* #param qrcode the qrcode to set
*/
public void setQrcode(byte[] qrcode) {
this.qrcode = qrcode;
}
/**
* #return the vendorno
*/
public int getVendorno() {
return vendorno;
}
/**
* #param vendorno the vendorno to set
*/
public void setVendorno(int vendorno) {
this.vendorno = vendorno;
}
}
Override the toString() method in the ProductEJBDTO to return the name.
public class ProductEJBDTO implements Serializable {
...
...
#Override
public String toString() {
return this.prodnam; // The product name you want to display.
}
}
Use the itemLabel attribute (docs):
<f:selectItems value="..." var="x" itemLabel="#{x.prodnam}"></f:selectItems>
(I assume the prodnam property is the desired display name; any EL expression will do though)