Fill a list from another activity

0

I am reading a JSON from the web, After parsing my JSON, I have loaded the data in an array in order to use them, my question is, how can I pass this data in another activity to fill a list ..

my code ..

new AsyncTask<Void, Void, Void>() {
                @Override
                protected Void doInBackground(Void... params) {
                    String url = "http://www.air-intra.com/apps/air-app/agregar.php?token=" + dato + "&codiart=" + cod + "";
                    HttpHandler she = new HttpHandler();
                    JSONObject jsonStr = she.makeServiceCall(url);

                    Log.e("CODEXES", String.valueOf(jsonStr));
                    try {
                        JSONObject jsonObj = new JSONObject(String.valueOf(jsonStr));
                        JSONArray contacts = jsonObj.getJSONArray("PRR");
                      //  prod.clear();
                        for (int i = 0; i < contacts.length(); i++) {
                            JSONObject c = contacts.getJSONObject(i);

                            String codig = c.getString("codigo");
                            String des = c.getString("descrip");
                            String prec = c.getString("precio");
                            String reglon = c.getString("renglon");

                            HashMap<String, String> contacto = new HashMap<>();
                            contacto.put("codigo", codig);
                            contacto.put("descrip", des);
                            contacto.put("precio", prec);
                            contacto.put("renglon", reglon);
                          //  reg = "" + reglon;
                            productos.add(contacto);





                    } }catch(JSONException e){
                        e.printStackTrace();
                    }

and in the onPostExecute I want to send the data of the array in a list of the other activity

 @Override
                protected void onPostExecute(Void aVoid) {

                   // Log.d("RESULTADO", toString());
                    Intent intent = new Intent(busqueda.this,Carrito.class);

// What can I do ...

startActivity(intent);
                    super.onPostExecute(aVoid);
                }
    
asked by Wid Maer 06.07.2017 в 16:23
source

1 answer

0

First I would recommend the use of retrofit which does the job of performing endpoint queries with a simpler syntax and It helps to get more information about the server's response, for example: 404, 500, among others ..

Responding to your question about how to pass the information from one activity to another we have the option to send these through the extra ones in the intent.

From your Activity:

    Intent intent = new Intent(busqueda.this,Carrito.class);

    intent.putExtra("string","hola mundo!");
    intent.putExtra("entero",0);
    intent.putExtra("real",0.5f);
    intent.putExtra("boolean", true);

    startActivity(intent);

and Receiving it in this way in the onCreate of the other Activity Cart in this way:

    Intent intent = getIntent();
    String string = intent.getStringExtra("string");
    //el segundo parametro es un default para la variable
    //en caso que no encuentre la variable
    int entero = intent.getIntExtra("entero",0);
    float real = intent.getFloatExtra("real",0.0f);
    boolean bool = intent.getBooleanExtra("bool", false);

In the case of sending non-primitive data, we have the option of making them Parcelable or Serializable.

The 2 meet the same function only that the parcelable you have to write a little more, I leave you an example here and Serializable is easier to implement but a bit more inefficient than Parcelable, example of Serializable here .

I hope I have helped you.

    
answered by 06.07.2017 в 16:42