Android对话框接口获取项目对话框视图

时间:2018-03-12 20:07:21

标签: android

大家好我是一个简单的程序,在FRAGMENT CLASS中显示一个对话框,上面有一个项目列表。 我使用AsyncTask类,解析我的json数据并在AlertDialog上发布结果,我的问题是当我选择一个项目时,他总是给我回复第一项..任何帮助请....这里是我的代码

  new AsyncTask<String, String, String>() {
        AlertDialog.Builder alertdialogbuilder;

        protected void onPreExecute() {
            super.onPreExecute();
            alertdialogbuilder = new AlertDialog.Builder(getActivity());
            alertdialogbuilder.setTitle("Que Categorias?");

        }

        @Override
        protected String doInBackground(String... strings) {
            // alertdialogbuilder.setTitle("De Que Lineas ?");
            final String url = "http://myurl.com";
            prod = new ArrayList<>();
            HttpHandler sh = new HttpHandler();
            JSONObject jsonStr = sh.makeServiceCall(url);
            JSONObject jsonObj = null;
            Log.d("REGLON", String.valueOf(jsonStr));
            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);
                    descripcion = c.getString("nombre");
                    String codigo=c.getString("codigo");

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

                    contacto.put("nombre", descripcion);
                    contacto.put("codigo",codigo);
                    prod.add(contacto);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(String s) {
            final View view = new View(getActivity());
            super.onPostExecute(s);

            SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity(), prod,
                    R.layout.item_fabricante,
                    new String[]{"nombre","codigo"},
                    new int[]{R.id.txtSeccion,R.id.textView2});
            alertdialogbuilder.setAdapter(simpleAdapter, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    TextView texto = (TextView) ((AlertDialog) dialogInterface).findViewById(R.id.textView2);
                    //TextView  = (TextView) myView.findViewById(R.id.txtSeccion);

                    String pepe = texto.getText().toString();
                    Log.d("REGLON", String.valueOf(pepe));
                    Toast.makeText(getActivity(),pepe,Toast.LENGTH_LONG).show();
                }
            });

            alertdialogbuilder.setCancelable(false);
            alertdialogbuilder.create();
            alertdialogbuilder.show();
        }


    }.execute();

0 个答案:

没有答案