addView List - The specified Child already has a Parent

Asked

Viewed 45 times

0

I’m getting the bug

"java.lang.Illegalstateexception: The specified Child already has a Parent. You must call removeView() on the Child’s Parent first."

even removing Parent from View.

Follows the code:

LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout root = (LinearLayout) linha.findViewById(R.id.ingredientes_sel);
List views = new ArrayList();

View view = inflater.inflate(R.layout.item_obs_carrinho, null);
view.setLayoutParams(
        new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));

if (tipo == 2 || tipo == 3) { //add
    for (int i = 0; i < objetos_add.get(0).length; i++) {
        if (!objetos_add.get(0)[i].equals("")) {
            if (view.getParent() != null)
                ((ViewGroup) view.getParent()).removeView(view);
            TextView n = (TextView) view.findViewById(R.id.item_ingred_carrinho);
            n.setText(objetos_add.get(0)[i]);

            TextView qtd = (TextView) view.findViewById(R.id.quantidade);
            qtd.setText(objetos_add.get(1)[i]);

            views.add(view);
        }
    }
}
if (tipo == 1 || tipo == 3) { //rem
    for (int i = 0; i < objetos_rem.get(0).length; i++) {
        if (!objetos_rem.get(0)[i].equals("")) {
            if (view.getParent() != null)
                ((ViewGroup) view.getParent()).removeView(view);
            TextView n = (TextView) view.findViewById(R.id.item_ingred_carrinho);
            n.setText(objetos_rem.get(0)[i]);

            TextView qtd = (TextView) view.findViewById(R.id.quantidade);
            qtd.setText("");

            ImageView add = (ImageView) view.findViewById(R.id.add_ou_rem);
            add.setImageResource(android.R.drawable.ic_delete);

            views.add(view);
        }
    }
}

for (int i = 0; i < views.size(); i++)
    root.addView((View) views.get(i)); /* <---- ERRO  */

What’s wrong with it?

I made this modification in the last loop but now it only displays the last element of the list "views":

for (int i = 0; i < views.size(); i++) {
    ViewGroup paren = (ViewGroup) ((ViewGroup) views.get(i)).getParent();
    if (paren != null) {
        paren.removeView((View) views.get(i));
    }
    root.addView((View) views.get(i));
}
  • Try on the last loop put root.removeView(views.get(i)); before root.addView((View) views.get(i));. Don’t forget to put these two lines in parentheses.

  • How so between parentheses? I left only these two lines inside the last loop, still showing only the last element of the list. I also put "view.setLayoutParams.." where it was before in the example I took from the internet that is before views.add(view). Nothing yet...

  • In this first line I had to give a cast for View because it was giving error between the types View (required) and Object (informed)

  • Solucionado no Stack em inglês: http://stackoverflow.com/q/42681805/5445596

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.