2

I am doing an Asynctask function that reads from json file. I want that on postExecute pass like string contact´s "nombre, categoria, hora, lugar..." to another Activity that I have. How can I get contact´s values on postExecute? Somebody can help me? This is my code

private class GetProgramaSC extends AsyncTask<Void, Void, Void> {


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(getActivity());
            if(idioma.equalsIgnoreCase("es")){  
                pDialog.setMessage("Por favor espere...");
            }
            else{
                pDialog.setMessage("Itxaron mesedez...");
            }
            pDialog.setCancelable(false);
            pDialog.show();

        }

        @Override
        protected Void doInBackground(Void... arg0) {

                try {

                    JSONObject jsonObj = new JSONObject(loadJSONFromAsset());

                    // Getting JSON Array node
                   eventos = jsonObj.getJSONArray("results");

                    // looping through All Contacts
                    for (int i = 0; i < eventos.length(); i++) {
                        JSONObject c = eventos.getJSONObject(i);

                        String categoria = c.getString(TAG_CATEGORIA);
                        String nombre = c.getString(TAG_NOMBRE);
                        String hora = c.getString(TAG_HORA);
                        String lugar = c.getString(TAG_LUGAR);
                        String fecha = c.getString(TAG_FECHA);
                        String coordenadas = c.getString(TAG_COORDENADAS);
                        String info = c.getString(TAG_INFO);
                        String imagen= c.getString(TAG_IMAGEN);

                        // tmp hashmap for single contact
                        HashMap<String, String> contact = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        contact.put("categoria", categoria);
                        contact.put("nombre", nombre);
                        contact.put("hora", hora);
                        contact.put("nombre_lugar", lugar);
                        contact.put("fecha", fecha);
                        contact.put("coordenadas", coordenadas);
                        contact.put("info", info);
                        contact.put("imagen", imagen);


                        // adding contact to contact list
                        eventosList.add(contact);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            return null;


        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();




        }

    }

Thank you so much

Androi
  • 35
  • 3

5 Answers5

2

Change your AsyncTask signature to:

private class GetProgramaSC extends AsyncTask<Void, Void, List<String>>

and then doInBackground will have to return List. So, return your eventos on end of doInBackground, and you will receive it onPostExecute.

private class GetProgramaSC extends AsyncTask<Void, Void, List<String>> {

...

    @Override
    protected List<String> doInBackground(Void... arg0) {

          ...

        return eventos;


    }

    @Override
    protected void onPostExecute(List<String> result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();

    }

}
Tomasz Czura
  • 2,414
  • 1
  • 14
  • 18
0

this is very simple pass data to postexecute and do with data whatever you want

  private class GetProgramaSC extends AsyncTask<String, String, String> {


            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                // Showing progress dialog
                pDialog = new ProgressDialog(getActivity());
                if(idioma.equalsIgnoreCase("es")){  
                    pDialog.setMessage("Por favor espere...");
                }
                else{
                    pDialog.setMessage("Itxaron mesedez...");
                }
                pDialog.setCancelable(false);
                pDialog.show();

            }

            @Override
            protected Void doInBackground(String... arg0) {



                return loadJSONFromAsset();


            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                // Dismiss the progress dialog

      try {

                        JSONObject jsonObj = new JSONObject(result);

                        // Getting JSON Array node
                       eventos = jsonObj.getJSONArray("results");

                        // looping through All Contacts
                        for (int i = 0; i < eventos.length(); i++) {
                            JSONObject c = eventos.getJSONObject(i);

                            String categoria = c.getString(TAG_CATEGORIA);
                            String nombre = c.getString(TAG_NOMBRE);
                            String hora = c.getString(TAG_HORA);
                            String lugar = c.getString(TAG_LUGAR);
                            String fecha = c.getString(TAG_FECHA);
                            String coordenadas = c.getString(TAG_COORDENADAS);
                            String info = c.getString(TAG_INFO);
                            String imagen= c.getString(TAG_IMAGEN);

                            // tmp hashmap for single contact
                            HashMap<String, String> contact = new HashMap<String, String>();

                            // adding each child node to HashMap key => value
                            contact.put("categoria", categoria);
                            contact.put("nombre", nombre);
                            contact.put("hora", hora);
                            contact.put("nombre_lugar", lugar);
                            contact.put("fecha", fecha);
                            contact.put("coordenadas", coordenadas);
                            contact.put("info", info);
                            contact.put("imagen", imagen);


                            // adding contact to contact list
                            eventosList.add(contact);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

 if (pDialog.isShowing())
                    pDialog.dismiss();




            }

        }

later add hashmap to bundle to pass activity

try as: in post execute

Bundle extras = new Bundle();
extras.putSerializable("HashMap",hashMap);
intent.putExtras(extras);

and in another Activity

Bundle bundle = this.getIntent().getExtras();

if(bundle != null) {
   hashMap = bundle.getSerializable("HashMap");
}
raj
  • 2,088
  • 14
  • 23
0

Passing a large amount of String data cannot be done with the help of intent to another activity, so after parsing the JSON you can store these in an Application class, which can be called anywhere inside the project.

Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77
0

Extend your class with Object (whatever you want as result) for result

public class GetProgramaSC extends AsyncTask<Void, Void, Object> {}

Call asyncTask with get() method, but keep in mind it will run on main thread.

Object result = new GetProgramaSC().get();

Hope it will help you!

Jagruttam Panchal
  • 3,152
  • 2
  • 15
  • 21
0
    @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();
            Intent i = new Intent(getActivity, NewActivity.class);
            i.putExtra("arraylist", eventolist);
            startActivity(i);
}

Then on new activity:

ArrayList<HashMap<String, String>> arraylist = (ArrayList<HashMap<String,String>>) getIntent().getSerializableExtra("arraylist");

Now just extract hashmap and its values from the arraylist in the new activity! eg:- arraylist.get(0).get("nombre");

Sumit Shetty
  • 112
  • 1
  • 9