Search code examples
javaandroidretrofitpojo

How to use a class?


I use retrofit for data transmission. And I'm confused. I have Pojo generated classes. But I do not know how to use the Destination class.

  public class NewOrderRequest {

    @SerializedName("phone")
    @Expose
    private String phone;

    public NewOrderRequest(String phone, List<Destination> destinations) 
    {
       super();
       this.phone = phone;
       this.destinations = destinations;
    }

    public String getPhone() {return phone;}

    public void setDestinations(List<Destination> destinations)
    {
    this.destinations = destinations;
    }

    }

CLASS DESTINATION:

public class Destination {

   @SerializedName("lat")
    @Expose
    private String lat;

    public Destination(String lat) {
    super();
    this.lat = lat;

    public String getLat() {
    return lat;
    }

    public void setLat(String lat) {
    this.lat = lat;
    }

    }

USE:

What should I pass to the setDestination method?

    NewOrderRequest newOrderRequest = new NewOrderRequest();
    newOrderRequest.setPhone("+911");
    newOrderRequest.setDestinations(????????);


    NetworkService.getInstance()
                .service()
                .newOrder(jsessionid, newOrderRequest)

This is what I tried:

  List<Destination> destination = null;
        destination.add();

Solution

  • You need to create a List<Destination. But in your attempt, you only declare the List, you never create one. Try this:

    List<Destination> destination = new ArrayList<>();
    destination.add(new Destination("lat"));
    newOrderRequest.setDestinations(destination);
    

    You also need to

    import java.util.ArrayList;