removeView () AlertDialog on Android

2

I'm trying to show a personalized list within an alertDialog, everything goes well until the user clicks on an item followed by the alertDialog is closed and when I want to show the alertDialog again I get this error:

The specified child already has a parent. You must call removeView() on the child's parent first.

This is my code:

AlertDialog dialog; //variable global
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.conductor);
    cargar_mensajes();
}

private void cargar_mensajes() {
    String url = url;
    JsonObjectRequest request = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>(){
        @Override
        public void onResponse(JSONObject response) {
            try{
                lista_mensajes = new ListView(Conductor.this);
                JSONObject res = new JSONObject(response.toString());
                JSONObject data = res.getJSONObject("data");
                JSONArray mensajes = data.getJSONArray("mensajes");
                for(int i = 0; i<mensajes.length();i++){
                    JSONObject item = mensajes.getJSONObject(i);
                    int id_mensaje = item.getInt("id_mensaje");
                    String mensaje_cond = item.getString("mensaje");
                    //LISTA PERSONALIZADA
                    datos_mensajes = new Datos_mensajes(id_mensaje, mensaje_cond);
                    arrayList.add(datos_mensajes);
                    Adaptador_mensajes adaptador_mensajes = new Adaptador_mensajes(Conductor.this,arrayList);
                    lista_mensajes.setAdapter(adaptador_mensajes);

                    lista_mensajes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            TextView txt_id_mensaje_conductor = (TextView)view.findViewById(R.id.txt_id_mensaje_conductor);
                            TextView txt_mensaje_conductor = (TextView)view.findViewById(R.id.txt_mensaje_conductor);
                            String id_mensaje_conductor_txt = txt_id_mensaje_conductor.getText().toString();
                            String mensaje_conductor_txt = txt_mensaje_conductor.getText().toString();

                            Toast.makeText(Conductor.this, mensaje_conductor_txt+" "+id_mensaje_conductor_txt, Toast.LENGTH_SHORT).show();
                            dialog.dismiss(); //CIERRO ALERTDIALOG
                        }
                    });
                    //FIN LISTA PERSONALIZADA
                }
            }catch (JSONException e){

            }
            onConnectionFinished();
        }
    }, new Response.ErrorListener(){
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(Conductor.this,"Error al cargar los mensajes",Toast.LENGTH_LONG).show();
            onConnectionFailed();
        }
    }){
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String>  params = new HashMap<String, String>();
            params.put("Content-Type", "application/json");
            params.put("Authorization", "Basic "+auth);
            return params;
        }
    };
    addToQueue(request);
}

@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
    getMenuInflater().inflate(R.menu.menu_conductor,menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.sms_conductor:
            AlertDialog.Builder builder = new AlertDialog.Builder(Conductor.this);
            builder.setCancelable(true);
            builder.setView(lista_mensajes);
            dialog = builder.create();
            dialog = builder.show();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

All this I do with a menu that I show in the upper right. everything is correct the only mistake is the one that I mention, how can I solve it?

Thank you in advance

    
asked by Dimoreno 16.05.2017 в 19:07
source

2 answers

0

The indicated problem:

  

The specified child already has a parent. You must call removeView ()   on the child's first parent.

At the beginning it will show correctly but the second time it will show the error. In this case you must create a new instance of the List to add it to your AlerDialog and obviously load their respective data.

  AlertDialog.Builder builder = new AlertDialog.Builder(Conductor.this);
            builder.setCancelable(true);

            //crear una nueva instancia.
            ListView lista_mensajes = new ListView(Conductor.this);

            builder.setView(lista_mensajes);
            dialog = builder.create();
            dialog = builder.show();
    
answered by 16.05.2017 / 22:42
source
0

I could solve it, what I did was remove the instance of my listview and therefore its adapter and the onitemclicklistener method and add it in my menu method as follows

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.sms_conductor:
                lista_mensajes = new ListView(Conductor.this);
                adaptador_mensajes = new Adaptador_mensajes(Conductor.this,arrayList);
                lista_mensajes.setAdapter(adaptador_mensajes);

                lista_mensajes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        TextView txt_id_mensaje_conductor = (TextView)view.findViewById(R.id.txt_id_mensaje_conductor);
                        TextView txt_mensaje_conductor = (TextView)view.findViewById(R.id.txt_mensaje_conductor);
                        String id_mensaje_conductor_txt = txt_id_mensaje_conductor.getText().toString();
                        String mensaje_conductor_txt = txt_mensaje_conductor.getText().toString();

                        Toast.makeText(Conductor.this, mensaje_conductor_txt+" "+id_mensaje_conductor_txt, Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                });
                AlertDialog.Builder builder = new AlertDialog.Builder(Conductor.this);
                builder.setCancelable(true);
                builder.setView(lista_mensajes);
                dialog = builder.create();
                dialog = builder.show();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
answered by 16.05.2017 в 23:27