I am trying to display a custom list inside an alertDialog, everything goes fine until when the user clicks on an item followed by it the alertDialog closes 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);
}
I do all this with a menu that I show in the upper right part. Everything is correct, the only error is the one I mentioned, how can I solve it?
I thank you in advance
The stated problem:
At first 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 its respective data.I was able to solve it, what I did was remove the instance of my listview and therefore its adapter and also the onitemclicklistener method and that I added in my menu method as follows