Spinner Json and Arraylist

Asked

Viewed 30 times

0

I’m starting on Android development and I’m having a tense doubt here. I have to return a Json like this:

[
   {
      "codTransportadora":295,
      "NomeTransportadora":"UTILISSIMO",
      "codEmpresa":3122
   },
   {
      "codTransportadora":1122,
      "NomeTransportadora":"UNILOG",
      "codEmpresa":3122
   },
   {
      "codTransportadora":273,
      "NomeTransportadora":"COOPERCARGA",
      "codEmpresa":3122
   },
   {
      "codTransportadora":352,
      "NomeTransportadora":"PATRUS",
      "codEmpresa":3122
   },
   {
      "codTransportadora":364,
      "NomeTransportadora":"FEDEX",
      "codEmpresa":3122
   },
   {
      "codTransportadora":516,
      "NomeTransportadora":"TRANSMAGNA",
      "codEmpresa":3122
   },
   {
      "codTransportadora":982,
      "NomeTransportadora":"OTD",
      "codEmpresa":3122
   },
   {
      "codTransportadora":1083,
      "NomeTransportadora":"KGT",
      "codEmpresa":3122
   },
   {
      "codTransportadora":1834,
      "NomeTransportadora":"TOMBINI",
      "codEmpresa":3122
   },
   {
      "codTransportadora":2367,
      "NomeTransportadora":"RAPIDO MAXEXPRESS",
      "codEmpresa":3122
   },
   {
      "codTransportadora":2382,
      "NomeTransportadora":"SETTOR",
      "codEmpresa":3122
   },
   {
      "codTransportadora":2636,
      "NomeTransportadora":"REITER",
      "codEmpresa":3122
   },
   {
      "codTransportadora":3938,
      "NomeTransportadora":"PATRUS TRANSPORTES URGENTES",
      "codEmpresa":3122
   },
   {
      "codTransportadora":6868,
      "NomeTransportadora":"SEQUOIA",
      "codEmpresa":3122
   },
   {
      "codTransportadora":7045,
      "NomeTransportadora":"IBL",
      "codEmpresa":3122
   },
   {
      "codTransportadora":7334,
      "NomeTransportadora":"GENEROSO",
      "codEmpresa":3122
   },
   {
      "codTransportadora":7340,
      "NomeTransportadora":"DV3",
      "codEmpresa":3122
   },
   {
      "codTransportadora":7344,
      "NomeTransportadora":"ENTREGA FACIL EXPRESS",
      "codEmpresa":3122
   },
   {
      "codTransportadora":7347,
      "NomeTransportadora":"TGESTIONA",
      "codEmpresa":3122
   },
   {
      "codTransportadora":7348,
      "NomeTransportadora":"RODOFLY",
      "codEmpresa":3122
   },
   {
      "codTransportadora":3366,
      "NomeTransportadora":"SANTINI",
      "codEmpresa":3122
   },
   {
      "codTransportadora":6374,
      "NomeTransportadora":"LOGIC LOGISTICA",
      "codEmpresa":3122
   },
   {
      "codTransportadora":294,
      "NomeTransportadora":"FASSINA",
      "codEmpresa":3122
   },
   {
      "codTransportadora":1472,
      "NomeTransportadora":"TRANSPOSERVS",
      "codEmpresa":3122
   },
   {
      "codTransportadora":7404,
      "NomeTransportadora":"SERVPORTO",
      "codEmpresa":3122
   },
   {
      "codTransportadora":3116,
      "NomeTransportadora":"ALDA TRANSPORTES",
      "codEmpresa":3122
   },
   {
      "codTransportadora":843,
      "NomeTransportadora":"CAMILO",
      "codEmpresa":3122
   },
   {
      "codTransportadora":7023,
      "NomeTransportadora":"LETSARA",
      "codEmpresa":3122
   },
   {
      "codTransportadora":6863,
      "NomeTransportadora":null,
      "codEmpresa":3122
   }
]

It’s a list of carriers, in the URL method, I just enter the CodEmpresa.

So I need to get the values of the fields NomeTransportadora and feed my Spinner. How could I do this?

I did the following:

Type type2 = new TypeToken<ArrayList<Get_Transportadora>>() {
                                        }.getType();
                                        final List<Get_Transportadora> objectsList = new Gson().fromJson(send.getResponse(), type2);

                                        ArrayAdapter<String>arrayAdapter;
                                        arrayAdapter = new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,);

                                        arrayAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item,****"Aqui eu creio que deveria passar o meu -objectsList-" mas continua dando erro***);
                                         spinner_transp.setAdapter(arrayAdapter);

1 answer

0

I would advise using the lib Jackson (https://github.com/FasterXML/jackson-core)

you can create a class that represents what comes from the server, and the lib transforms for you into a JAVA object btw here (https://github.com/BeholderDEV/MagicRBC/tree/master/MagicAPIBot) there’s an example of how I used it to make something like letters, but it would be something like this:

public class Transportadora{
    private int codTransportadora;
    private String NomeTransportadora;
    private int codEmpresa;

    // getters e setters
}

hence :

public class TransportadorasCollection {
    private Transportadora[] transportadoras;

    public Transportadora[] getTransportadoras() {
        return transportadoras;
    }

    public void setTransportadoras(Transportadora[] transportadoras) {
        this.transportadoras= transportadoras;
    }

}

and then:

public void run(){
        mapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
        try {

            TransportadorasCollection cl = mapper.readValue(new File("/caminho"), TransportadorasCollection .class)
         .....
        }

Finally; lib documentation helps too

Browser other questions tagged

You are not signed in. Login or sign up in order to post.