问题:使用ArrayList添加视图(Issue: addView with ArrayList)

编程入门 行业动态 更新时间:2024-10-11 11:13:44
问题:使用ArrayList添加视图(Issue: addView with ArrayList)

我收到以下错误:

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)); } }

更多推荐

本文发布于:2023-08-04 22:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1422683.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   ArrayList   addView   Issue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!