Spinner Json and ArrayList

0

I'm starting in Android development and I'm having a tense question here. I have a Json return 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 CodEmpresa .

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

I've done 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);
    
asked by anonymous 17.03.2018 / 03:33

1 answer

0

I would advise using Jackson ( link )

You can create a class that represents what comes from the server, and lib transforms you into a JAVA object btw here ( link ) has an example of how I used this to do something like letterheads, but it would be something more or less like this:

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

    // getters e setters
}

dai:

public class TransportadorasCollection {
    private Transportadora[] transportadoras;

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

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

}

then:

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

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

Anyway; the lib documentation help tb

    
17.03.2018 / 04:00