1

i have fragment that has scrollview as a parent of layout and in scrollview i have a LinearLayout . when i'm trying to programmatically add view to that LinearLayout but i get the error " The specified child already has a parent. You must call removeView() on the child's parent first."

this is my layout

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="ltr"
tools:context="ir.pnuopen5.application.fragment.LeaugeFragment">

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <TextView
        android:id="@+id/tv_LeagueFragment_title_lastNews"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/last_news"
        android:textSize="17sp"
        android:layout_gravity="right"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/view1"
        android:layout_alignEnd="@+id/view1" />

    <View
        android:id="@+id/view1"
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:layout_marginTop="10dp"
        android:background="@color/line"
        android:layout_below="@+id/tv_LeagueFragment_title_lastNews"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lv_LeagueFragment_lastNews"
        android:layout_below="@+id/view1"
        android:orientation="horizontal"/>

    ...

and this is my java code

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_leauge, container, false);

    ...

    LinearLayout newsListView = (LinearLayout)view.findViewById(R.id.lv_LeagueFragment_lastNews);
    View newsListRow;
    TextView title,time;


    for (int i = 0;i<newsList.size();i++){
        newsListRow  =  inflater.inflate(R.layout.list_news_row0, container, false);
        title = (TextView)newsListRow.findViewById(R.id.rowNewsTitle);
        time = (TextView)newsListRow.findViewById(R.id.rowNewsTime);
        title.setText(newsList.get(i).getTitle());
        time.setText(newsList.get(i).getTime());
        newsListView.addView(newsListView);
    }

    ...

    return view;
}
7
  • 1
    inflater.inflate(R.layout.list_news_row0, container, false); => container? not newsListView? newsListView.addView(newsListView); <= seriously you wana add view to itself? also use ListView instead Commented Feb 24, 2015 at 3:33
  • newsListView.addView(newsListView); ??!! Commented Feb 24, 2015 at 3:36
  • @Selvin i'm need a something like listview but that shouldn't be scrollable Commented Feb 24, 2015 at 3:37
  • before adding the view , remove it by calling newsListView.removeView(view); Commented Feb 24, 2015 at 3:41
  • @MortezaSoleimani ohhh ! This was causing the problems. ! Thank you for your help Commented Feb 24, 2015 at 3:43

3 Answers 3

2

In the code snippet newsListView.addView(newsListView), you are adding a view to itself. I think what you meant to do was:

newsListView.addView(newsListRow);

...to put it back in context...

for (int i = 0;i<newsList.size();i++){
    newsListRow  =  inflater.inflate(R.layout.list_news_row0, container, false);
    title = (TextView)newsListRow.findViewById(R.id.rowNewsTitle);
    time = (TextView)newsListRow.findViewById(R.id.rowNewsTime);
    title.setText(newsList.get(i).getTitle());
    time.setText(newsList.get(i).getTime());
    newsListView.addView(newsListRow);
}
Sign up to request clarification or add additional context in comments.

Comments

0

It's obvious that you get that error. Look you are inflating an View, for first time when your loop runs it adds that view in your linear layout. When next time your loop runs it takes the same object of that view which you have previously added. As this is a static view it wont create a new object when you initialize it.

You should use dynamic View.

Following is your answer

    for(int i = 0;i<newsList.size();i++) {
        LinearLayout newsListRow  = new LinearLayout(this);
        TextView title = new TextView(this);
        TextView time = new TextView(this);

        title.setText(newsList.get(i).getTitle());
        time.setText(newsList.get(i).getTime());

        newsListRow.addView(title);
        newsListRow.addView(time);

        newsListView.addView(newsListView);
    }

You can add LayoutParameter to make it look like your static row view.

That's it... Enjoy... :-)

1 Comment

Actually inflating the same xml in a loop WILL create a new object each time. It's not necessary to do it in code instead of xml.
0

Before adding the view , remove it by calling

newsListView.removeView(view);

Comments

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.