2017-03-08 32 views
0

,我发现了以下错误:问题:addView与ArrayList的

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

我从视图中删除父,但它不能正常工作。

代码:

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 */ 

有什么不对?

编辑

基于这样一个问题:Dynamic layout inflator within a loop

+0

你能解释一下你在做什么吗? –

+0

创建产品列表,使用哪种模型的XML布局。此布局的内容是一个包含2个文本和一个图像的LinearLayout(将此扩展) –

+0

如果您需要列表,为什么不使用ArrayAdapter? –

回答

0

你们这样做,一旦

View view = inflater.inflate(R.layout.item_obs_carrinho, null); 

然后你叫root.addView(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)); 
    } 
} 
+0

我明白了。但是,我也需要与objetos_rem进行交互......如何在不多次调用addView的情况下执行此操作? –

+0

好的,所以你需要两个循环。查看更新 –

+0

好的,我会尽快测试。 –