1

how can i add more same layout more then one

View v = (LinearLayout) inflater.inflate(R.layout.tab_frag2_layout,container, false);
     RelativeLayout tv = (RelativeLayout) inflater.inflate(R.layout.post_layout,container,false);
     RelativeLayout tv2 = (RelativeLayout) inflater.inflate(R.layout.post_layout,container,false);

    ((LinearLayout) v).addView(tv);
    ((LinearLayout) v).addView(tv);
    ((LinearLayout) v).addView(tv);
    ((LinearLayout) v).addView(tv);
    ((LinearLayout) v).addView(tv);

if i do this it give me error and then if i do this it shows it only one time

View v = (LinearLayout) inflater.inflate(R.layout.tab_frag2_layout,container, false);
     RelativeLayout tv = (RelativeLayout) inflater.inflate(R.layout.post_layout,container,false);
     RelativeLayout tv2 = (RelativeLayout) inflater.inflate(R.layout.post_layout,container,false);

    ((LinearLayout) v).addView(tv);
    ((LinearLayout) v).addView(tv2);

what to do please help

1
  • By the way - your layout is really bad, because there is a lot of nested layouts. It's better to move all this stuff into single RelativeLayout Commented Aug 29, 2012 at 12:17

1 Answer 1

2

First of all, there is no point in cast:

View v = (LinearLayout) inflater.inflate(R.layout.tab_frag2_layout, container, false);

Remove cast, or change type of v to LinearLayout.

It will be better if you'll provide your log cat with error message, but I supose that you're a getting ClassCastException here:

((LinearLayout) v).addView(tv);

Maybe your v isn't LinearLayout? If you're not sure, try this one instead:

((ViewGroup) v).addView(tv);

EDIT

When you're inflating layout the instance of View is created. One View can't have more than 1 parent, so when you're doing this:

.addView(tv);
.addView(tv);

OS thinks that you're trying to add another parent to tv (it's prohibited, so it's throws an Exception)

Sign up to request clarification or add additional context in comments.

4 Comments

@nomee.tec use ListView instead. If for some reasons you can't, then do same operation in loop (but I strongly recommend you to use ListView).
i cant use ListView Because i m creating sort of Fb comment wall so there will be two lists 1.comment and 2. subcomment
@nomee.tec you still able to use ListView. It may be not scrollable, if you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.