我收到以下错误:
java.lang.IllegalStateException:指定的子项已经有父项。 您必须先调用子对象的父对象的removeView()。
我从视图中删除父,但它不起作用。
代码:
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 */哪里不对?
编辑
基于这个问题: 循环内的动态布局充气机
I'm getting the following error:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
I remove Parent from View but it not working.
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 is wrong?
EDIT
Based on this question: Dynamic layout inflator within a loop
最满意答案
你曾经这样做过
View view = inflater.inflate(R.layout.item_obs_carrinho, null);然后你多次调用root.addView(view) ,因此以前添加的任何view都是完全相同的,因此会有一个父view 。
您需要膨胀多个视图然后添加到根目录。
所以,你可以提取出这种方法
private View getCustomView(LayoutInflater inflater, int tipo, List<String[]> data, int pos) { // Inflate View view = inflater.inflate(R.layout.item_obs_carrinho, null); view.setLayoutParams( new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); // Remove parent if (view.getParent() != null) ((ViewGroup) view.getParent()).removeView(view); // Find Views TextView n = (TextView) view.findViewById(R.id.item_ingred_carrinho); TextView qtd = (TextView) view.findViewById(R.id.quantidade); // Set views based on type if (tipo == 2 || tipo == 3) { // add n.setText(data.get(0)[pos]); qtd.setText(data.get(1)[pos]); } else if (tipo == 1 || tipo == 3) { // rem n.setText(data.get(0)[pos]); qtd.setText(""); ImageView add = (ImageView) view.findViewById(R.id.add_ou_rem); add.setImageResource(android.R.drawable.ic_delete); } return view; }然后在这两个循环之间调用它。
final LayoutInflater inflater = LayoutInflater.from(MainActivity.this); // tipo = 2 or 3 for (int i = 0; i < objetos_add.get(0).length; i++) { if (!objetos_add.get(0)[i].isEmpty()) { root.addView(getCustomView(inflater, tipo, objetos_add, i)); } } // tipo = 1 or 3 for (for int i = 0; i < objetos_rem.get(0).length; i++) { if (!objetos_rem.get(0)[i].isEmpty()) { root.addView(getCustomView(inflater, tipo, objectos_rem, i)); } }You did this once
View view = inflater.inflate(R.layout.item_obs_carrinho, null);And then you called root.addView(view) multiple times, therefore any view you added previously is the exact same, and therefore will have a parent.
You need to inflate multiple views that you then add to the root.
So, you can extract out this method
private View getCustomView(LayoutInflater inflater, int tipo, List<String[]> data, int pos) { // Inflate View view = inflater.inflate(R.layout.item_obs_carrinho, null); view.setLayoutParams( new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); // Remove parent if (view.getParent() != null) ((ViewGroup) view.getParent()).removeView(view); // Find Views TextView n = (TextView) view.findViewById(R.id.item_ingred_carrinho); TextView qtd = (TextView) view.findViewById(R.id.quantidade); // Set views based on type if (tipo == 2 || tipo == 3) { // add n.setText(data.get(0)[pos]); qtd.setText(data.get(1)[pos]); } else if (tipo == 1 || tipo == 3) { // rem n.setText(data.get(0)[pos]); qtd.setText(""); ImageView add = (ImageView) view.findViewById(R.id.add_ou_rem); add.setImageResource(android.R.drawable.ic_delete); } return view; }And then call it between those two loops.
final LayoutInflater inflater = LayoutInflater.from(MainActivity.this); // tipo = 2 or 3 for (int i = 0; i < objetos_add.get(0).length; i++) { if (!objetos_add.get(0)[i].isEmpty()) { root.addView(getCustomView(inflater, tipo, objetos_add, i)); } } // tipo = 1 or 3 for (for int i = 0; i < objetos_rem.get(0).length; i++) { if (!objetos_rem.get(0)[i].isEmpty()) { root.addView(getCustomView(inflater, tipo, objectos_rem, i)); } }更多推荐
发布评论