How can I show the results of a JSON in an AlertDialog?

0

I'm trying to show the results of my Json in an AlertDialog, using the AsyncTask class, I managed to process the url, but I have no idea how to show the final results within a listView, in my onPostExecute ... Some help for favor there is my code ...

new AsyncTask<String, String, String>() {
        AlertDialog.Builder alertdialogbuilder ;
        protected void onPreExecute() {
            super.onPreExecute();
            alertdialogbuilder = new AlertDialog.Builder(getActivity());
            alertdialogbuilder.setTitle("Que Lineas Desees?");

        }
        @Override
        protected String doInBackground(String... strings) {
           // alertdialogbuilder.setTitle("De Que Lineas ?");
            final String url = "http://mirUrl.com";
            prod = new ArrayList<>();
            HttpHandler sh = new HttpHandler();
            JSONObject jsonStr = sh.makeServiceCall(url);
            JSONObject jsonObj = null;
            try {
                jsonObj = new JSONObject(String.valueOf(jsonStr));
                JSONArray contacts = jsonObj.getJSONArray("CATEGORIAS");
                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);
                    String descripcion = c.getString("nombre");

                    HashMap<String, String> contacto = new HashMap<>();

                    contacto.put("nombre", descripcion);

                    prod.add(contacto);
                    Log.d("REGLON", String.valueOf(prod));
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
     return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            // aca deberia mostrar el resultado final  supongo...

            AlertDialog dialog = alertdialogbuilder.create();
            dialog.show();
        }

    }.execute();
    
asked by user62207 12.03.2018 в 16:26
source

2 answers

1

I managed to show the results in my JSON in the AlertDialog by joining AsyncTask ... there I left the answer of my onPostExecute ...

   @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity(), prod,
                    R.layout.item_fabricante,
                    new String[]{"nombre"},
                    new int[]{R.id.txtSeccion});
            alertdialogbuilder.setAdapter(simpleAdapter, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int itemIndex) {
                //    Hace lo que tu quieras..
                }
            });
            alertdialogbuilder.setCancelable(false);
            alertdialogbuilder.create();
            alertdialogbuilder.show();
        }


    }.execute();
    
answered by 12.03.2018 в 18:27
-1

Reviewing your code within doInBackground() you add values to a ArrayList called prod therefore in your AlertDialog you can show the values in this way:

 AlertDialog dialog = alertdialogbuilder.create();
 dialog.setMessage(prod.toString());
 dialog.show();
    
answered by 12.03.2018 в 16:45