0

I have an activity where linearlayouts are added dynamically into a base linearlayout. You'll understand from my code, but I'm trying to resuse the same "addBlock" method to add more data in the activity. If you can help me out with this, I would appreciate it! Thanks!

My code:

private void addData01() {
        wii=R.drawable.img01;
        d=getString(R.string.one_d); //this is later set to a seperate textview
        //START OF BLOCK 1//
        b1_t = getString(R.string.Title1);
        b1 = new String[]{"DATA WILL GO HERE"};
        b1_i = new String[]{"DATA WILL GO HERE"};
        addBlock();
        //START OF BLOCK 2//
        b1_t = getString(R.string.Title2);
        b1 = new String[]{"MORE DATA WILL GO HERE"};
        b1_i= new String[]{"MORE DATA WILL GO HERE"};
        addBlock();
    }

    private void addBlock() {

        LayoutInflater inflater = LayoutInflater.from(this);
        for(int i = 0 ; i < b1.length ; i++)
        {
            View childView = inflater.inflate(R.layout.two_row, null,false);       

            TextView tv = (TextView)childView.findViewById(R.id.one);
            TextView tv2 = (TextView)childView.findViewById(R.id.two);
            tv.setText(Html.fromHtml(b1[i]));
            tv2.setText(Html.fromHtml(b1_i[i]));
            b_ll.addView(b1t);
            b_ll.addView(childView);
        }       
    }

However when I do this I get the following error:

08-05 21:18:15.462: E/AndroidRuntime(4241): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

I have tried looking at this How to add same view to parent multiple times by inflating it only once but the solutions there did not seem to work

Community
  • 1
  • 1
cnfw
  • 770
  • 2
  • 11
  • 28
  • Where are b1t and childView declared? It looks like you only have one instance of those views but are trying to add it multiple times. If you want to add them dynamically you will have to create another instance when you call addBlock, otherwise you will get the IllegalStateException that you're having trouble with. – Terhands Aug 05 '14 at 21:34
  • The problem is: `b_ll.addView(b1t);`. `b1t` is defined elsewhere. And its probably fine on the first iteration of your `for-loop`. On the second iteration, you try to add `b1t` to `b_ll` _again_. The exception is telling you that you cannot do that. – Vikram Aug 05 '14 at 21:35
  • childView is declared in the for method... 7 lines up from where you read it there. b1t is a textview declared as `String b1t` just before onCreate – cnfw Aug 05 '14 at 21:36
  • @Vikram How would I fix this? If you could add an answer, that would be great :) – cnfw Aug 05 '14 at 21:37
  • To fix it just create a new instance of b1t in the addBlock method rather than in onCreate of the activity. Also make sure that you are always adding a new instance of childView in your addBlock - so you could add a method to createChildView which returns a new instance of childView, and do the same with b1t. – Terhands Aug 05 '14 at 21:40
  • 1
    `childView` is fine. You need to move the creation code for `b1t` inside the for-loop. – Vikram Aug 05 '14 at 21:41
  • so put in the for-loop: `b1t = new TextView(this);`? (sorry for asking instead of checking, but I'm now off my PC, so unable to test) – cnfw Aug 05 '14 at 21:43
  • b1t is part of the overall layout? Can you post the layout that you're inflating? – Terhands Aug 05 '14 at 21:46
  • @Terhands nope, sorry, made a mistake and confused it with another project. Its a new TextView, and my comment has been updated – cnfw Aug 05 '14 at 21:47
  • 1
    @cw1998 creating b1t inside the loop should fix the problem – Terhands Aug 05 '14 at 21:49
  • I can confirm that the problem is fixed. Thanks for your help everyone! – cnfw Aug 05 '14 at 21:57

0 Answers0